我在我的应用中实现了应用内购买。我使用了in-app billing版本3的"TrivialDrive"示例,并使用静态响应测试应用程序,一切都按预期工作。
但是当我从市场下载应用程序并成功购买后,应用程序会崩溃。
我的代码是处理成功购买的。
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
if (result.isFailure()) {
complain("Error purchasing: " + result);
}
if (purchase != null) {
if (purchase.getSku().equals(Constants.IN_APP_PURCHASE_PRODUCT_ID)) {
// bought the premium upgrade!
String message = getResources().getString(R.string.app_purchase_successful_msg);
mIsPremium = true;
updateUi(mIsPremium);
successAlert(message);
}
}
}
};
我已经设置了成功购买的消息,但是当我成功购买后,我看不到这个消息(也就是说那些代码没有执行),应用程序在成功付款后崩溃。
当重新启动应用程序并尝试购买商品时,它会显示已经购买过了。
我从开发者控制台得到了崩溃报告。
02-09 02:01:13.181: E/AndroidRuntime(11530): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10001, result=-1, data=Intent { (has extras) }} to activity {com.eknathkadam.grammaruplite/com.webrich.base.ui.GoogleInAppPurchaseActivity}: java.lang.NullPointerException
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.os.Looper.loop(Looper.java:137)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-09 02:01:13.181: E/AndroidRuntime(11530): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 02:01:13.181: E/AndroidRuntime(11530): at java.lang.reflect.Method.invoke(Method.java:511)
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-09 02:01:13.181: E/AndroidRuntime(11530): at dalvik.system.NativeStart.main(Native Method)
**02-09 02:01:13.181: E/AndroidRuntime(11530): Caused by: java.lang.NullPointerException
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.google.billing.Base64.decode(Base64.java:434)**
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.google.billing.Security.generatePublicKey(Security.java:87)
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.google.billing.Security.verifyPurchase(Security.java:66)
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.google.billing.IabHelper.handleActivityResult(IabHelper.java:437)
02-09 02:01:13.181: E/AndroidRuntime(11530): at com.webrich.base.ui.GoogleInAppPurchaseActivity.onActivityResult(GoogleInAppPurchaseActivity.java:238)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
02-09 02:01:13.181: E/AndroidRuntime(11530): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
02-09 02:01:13.181: E/AndroidRuntime(11530): ... 11 more
02-09 02:02:20.610: E/JavaBinder(250): !!! FAILED BINDER TRANSACTION !!!
02-09 02:02:20.638: E/MountService(250): Listener dead
02-09 02:02:20.638: E/MountService(250): Listener dead
02-09 02:02:22.028: E/InputDispatcher(250): channel '41f313f0 com.android.vending/com.android.vending.AssetBrowserActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
02-09 02:03:59.884: E/PowerManagerService(250): Excessive delay setting brightness: 198ms, mask=2
在这行代码中,onActivityResult()方法抛出了空指针异常。
if (!mHelper.handleActivityResult(requestCode, resultCode, data))
方法就是这样。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
这段代码在静态响应时运行良好,但在从市场购买时崩溃。