IabHelper.startSetup() 导致 NullPointerException
2022-09-02 02:23:15
我的应用使用应用内计费,并且在我的手机上工作正常 ()。但是,当我在模拟器()上运行它时,它在创建帮助程序以在应用程序购买中执行时不断崩溃。它抛出一个:Android 2.3.6
Android 4.4.2
NullPointerException
mHelper = new IabHelper(MainActivity.this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d("x", "In-app Billing setup failed: " + result);
}
else {
Log.d("x", "In-app Billing is set up OK");
}
}
});
这个答案说我应该把上面的代码放在一个try-block中,并将捕获放在它下面的答案中。这样做时我遇到错误,推荐的解决方案是将其也放在尝试捕获块中。即使我这样做了,它仍然崩溃了。
谁能告诉我怎么做?我完全被困住了。
堆栈跟踪:
12-24 10:59:51.908: E/AndroidRuntime(2273): FATAL EXCEPTION: main
12-24 10:59:51.908: E/AndroidRuntime(2273): Process: com.sandradiependaal.mediatie, PID: 2273
12-24 10:59:51.908: E/AndroidRuntime(2273): java.lang.NullPointerException
12-24 10:59:51.908: E/AndroidRuntime(2273): at com.sandradiependaal.meditatie.util.IabHelper.startSetup(IabHelper.java:267)
12-24 10:59:51.908: E/AndroidRuntime(2273): at com.sandradiependaal.meditatie.MainActivity$4.onItemClick(MainActivity.java:142)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.widget.AbsListView$3.run(AbsListView.java:3638)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.os.Handler.handleCallback(Handler.java:733)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.os.Handler.dispatchMessage(Handler.java:95)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.os.Looper.loop(Looper.java:136)
12-24 10:59:51.908: E/AndroidRuntime(2273): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-24 10:59:51.908: E/AndroidRuntime(2273): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 10:59:51.908: E/AndroidRuntime(2273): at java.lang.reflect.Method.invoke(Method.java:515)
12-24 10:59:51.908: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-24 10:59:51.908: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-24 10:59:51.908: E/AndroidRuntime(2273): at dalvik.system.NativeStart.main(Native Method)
IabHelper.java:
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
// service available to handle that Intent
mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}