InApp for Android exception exceptions

I implemented an inApp purchase in my application, but sometimes it gives me NPE, below is the stack trace. I can also post code if anyone is 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 is the 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); 

onPress the purchase button

 if (!mBillingService.checkBillingSupported()) { showDialog(DIALOG_CANNOT_CONNECT_ID); } mBillingService.requestPurchase("android.test.purchased", null); 
+7
source share
2 answers

In your BillingService.java onStart method for zero intent like

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

I believe this is due to zero intention. Give it a try!

+12
source

I found the right way to fix this. Well, actually it depends on how you look at it. If you do not want your service to be restarted after its process has been killed, 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 want to want the service to restart every time it was killed, then the answer you choose will be for you, since Android will restart your service after it is killed with zero intent. Again, see the Link I provided.

Oh and onStart are out of date.

+7
source

All Articles