简体   繁体   中英

Android InApp Purchase Null pointer exception

I had implemented inApp purchase in my application but sometimes it gives me NPE, below is stack trace. I can post the code also if anyone interested.

java.lang.RuntimeException: Unable to start service com.market.BillingService@48400380 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
at android.app.ActivityThread.access$3600(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.market.BillingService.handleCommand(BillingService.java:369)
at com.market.BillingService.onStart(BillingService.java:359)
at android.app.Service.onStartCommand(Service.java:420)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
... 10 more
java.lang.NullPointerException
at com.market.BillingService.handleCommand(BillingService.java:369)
at com.market.BillingService.onStart(BillingService.java:359)
at android.app.Service.onStartCommand(Service.java:420)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
at android.app.ActivityThread.access$3600(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)

Here goes relevant code

@Override
protected void onStart() {
    super.onStart();
    ResponseHandler.register(mDungeonsPurchaseObserver);
}
@Override
protected void onStop() {
    super.onStop();
    ResponseHandler.unregister(mDungeonsPurchaseObserver);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mBillingService.unbind();
}

And in OnCreate()

mDungeonsPurchaseObserver = new WMBPurchaseObserver(mHandler);

mBillingService = new BillingService();
mBillingService.setContext(BuyModel.this);
ResponseHandler.register(mDungeonsPurchaseObserver);

onClick of buy Button

if (!mBillingService.checkBillingSupported())
{
    showDialog(DIALOG_CANNOT_CONNECT_ID);
}
mBillingService.requestPurchase("android.test.purchased", null);

In your BillingService.java onStart method guard for null intent like this

 if (null != intent) {
        handleCommand(intent, startId);
 }

I believe this is caused by null intent. Try out!

I found the correct way to fix this. Well actually depends on how you look at it. If you don't want your service to be restarted after its process is killed then you need to override onStartCommand and return START_NOT_STICKY. Like this:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   handleCommand(intent, startId);
   return START_NOT_STICKY;
}

See START_NOT_STICKY . If you do want the service to be restarted every time it is killed then the current chosen answer is the one for you since Android will restart your service after it is killed with a null intent. Again, see the link I provided.

Oh and onStart is deprecated.

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