简体   繁体   中英

Strange null pointer exception after in-app purchase successful (Android)

I've written an app with in app billing (IAB). Everything seems to work fine, except one thing. If I instigate a purchase, the Google Play IAB purchase appears and I purchase an item fine, then the Purchase Complete window appears, and then disappears fine. However, my main app doesn't seem to start animating again and after a few seconds, the app crashes. My logcat output says their is a null pointer exception (related to my own coding - nothing Android specific)

        try
        {
            Bundle buyIntentBundle = mService.getBuyIntent(3, mActivity.getPackageName(), (String)args[1], "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");

            if(buyIntentBundle.getInt("RESPONSE_CODE") == BILLING_RESPONSE_RESULT_OK)
            {
                PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");

                try
                {
                    mActivity.startIntentSenderForResult(pendingIntent.getIntentSender(),
                               1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
                               Integer.valueOf(0));
                }
                catch (SendIntentException e)
                {
                    Log.v("MYINFO", "Error purchasing item.");
                    e.printStackTrace();
                }
            }
            else
            {
                Log.v("MYINFO", "Error purchasing item.");

                showResponse(buyIntentBundle.getInt("RESPONSE_CODE"));
            }
        }
        catch (RemoteException e1)
        {
            e1.printStackTrace();
        }

This code is within my own AsyncTask purchasing class, and the mActivity is the main Activity. The code

  mActivity.startIntentSenderForResult(...)

calls the 'OnActivityResult(...)' fine in the main Activity, but then the crash occurs.

Any ideas what I might be doing wrong?

Thank you.

My logcat output it:

V/MediaPlayer[Native](12951): start
E/MediaPlayer[Native](12951): start called in state 0
V/MediaPlayer[Native](12951): message received msg=100, ext1=-38, ext2=0
E/MediaPlayer[Native](12951): error (-38, 0)
D/MediaPlayer[Native](12951): calling notify_player_state: 0
V/MediaPlayer[Native](12951): callback application
V/MediaPlayer[Native](12951): back from callback
D/MediaPlayer[JAVA](12951): broadcasting MEDIA_CHANGE_PLAYER_STATE(with context) : 0x0
E/MediaPlayer[Native](12951): invoke failed: wrong state 0
D/MediaPlayer[JAVA](12951): broadcasting MEDIA_CHANGE_PLAYER_STATE(with context) : state = 0
V/MediaPlayer[Native](12951): MediaPlayer::setVolume(0.750000, 0.750000)
E/MediaPlayer[JAVA](12951): Error (-38,0)
V/MediaPlayerService(  301): [669] setVolume(0.750000, 0.750000)
V/AudioSink(  301): setVolume(0.750000, 0.750000)
W/dalvikvm(12951): threadid=33: thread exiting with uncaught exception (group=0x41b5ee48)
E/AndroidRuntime(12951): FATAL EXCEPTION: GLThread 22274
E/AndroidRuntime(12951): Process: com.mystuff.grame, PID: 12951
E/AndroidRuntime(12951): java.lang.NullPointerException
E/AndroidRuntime(12951):    at com.mystuff.grame.core.SpriteBatch.add(SpriteBatch.java:50)
E/AndroidRuntime(12951):    at com.mystuff.grame.core.Level.draw(Level.java:360)
E/AndroidRuntime(12951):    at com.mystuff.grame.core.Grame.drawGame(Grame.java:245)
E/AndroidRuntime(12951):    at com.mystuff.grame.core.Grame.onDrawFrame(Grame.java:178)
E/AndroidRuntime(12951):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
E/AndroidRuntime(12951):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
W/ActivityManager(  972):   Force finishing activity com.mystuff.grame/.MainActivity
D/ActivityManager(  972): setFocusedStack: mFocusedStack=ActivityStack{439390b8 stackId=1, 21 tasks}
V/ActivityManager(  972): Moving to PAUSING: ActivityRecord{43d96740 u0 com.mystuff.grame/.MainActivity t421 f}
V/MediaPlayer[Native](12951): stop
E/MediaPlayer[Native](12951): stop called in state 0
V/MediaPlayer[Native](12951): message received msg=100, ext1=-38, ext2=0
E/MediaPlayer[Native](12951): error (-38, 0)
D/MediaPlayer[Native](12951): calling notify_player_state: 0
V/MediaPlayer[Native](12951): callback application
V/MediaPlayer[Native](12951): back from callback
V/ActivityManager(  972): Moving to PAUSED: ActivityRecord{43d96740 u0 com.mystuff.grame/.MainActivity t421 f} (pause complete)
V/ActivityManager(  972): Moving to STOPPING: ActivityRecord{43d96740 u0 com.mystuff.grame/.MainActivity t421 f} (finish requested)
I/ActivityManager(  972): resumeTopActivitiesLocked(): target Stack:ActivityStack{439390b8 stackId=1, 21 tasks}
D/ActivityManager(  972): resumeTopActivityLocked: Launching home next
V/ActivityManager(  972): moveHomeStack:

It must have something to do with the in-app purchasing as it doesn't happen until the in app purchase completes... it seems the in app purchase flow is causing some of my variables to become null.

YOu are probably right. When the IAP dialog is opened, I think onPause is triggered. If onPause is triggered that means a lot of variables is set to null and shut down like the MediaPlayer.

Alternativly, the MediaPlayer(which I think is the cause) is set to a forced pause when the IAP dialog shows up. What you could try to do is pause the MediaPLayer when the purchase flow is initialized(or when a button is pressed that triggers it) and reininitialize when it is done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM