我在我的应用程序中实现了inApp购买,但有时它给了我NPE,下面是堆栈跟踪。如果有兴趣的话,我也可以发布代码。
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)
这里是相关代码
@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();
}
在OnCreate()
中mDungeonsPurchaseObserver = new WMBPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(BuyModel.this);
ResponseHandler.register(mDungeonsPurchaseObserver);
点击购买按钮
if (!mBillingService.checkBillingSupported())
{
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
mBillingService.requestPurchase("android.test.purchased", null);
答案 0 :(得分:12)
在您的BillingService.java onStart方法中,保护此类
的null意图 if (null != intent) {
handleCommand(intent, startId);
}
我认为这是由零意图引起的。试试吧!
答案 1 :(得分:7)
我找到了解决此问题的正确方法。那实际上取决于你如何看待它。如果您不希望在其进程被终止后重新启动服务,则需要覆盖onStartCommand并返回START_NOT_STICKY。像这样:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent, startId);
return START_NOT_STICKY;
}
见START_NOT_STICKY。如果您执行希望每次杀死服务时重新启动该服务,那么当前选择的答案就是您的答案,因为Android将在使用空意图终止后重新启动您的服务。再次,请参阅我提供的链接。
哦,onStart已被弃用。