Android InApp Purchase Null pointer exception

こ雲淡風輕ζ 提交于 2020-01-01 09:24:09

问题


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);

回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/6759802/android-inapp-purchase-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!