java – NullPointerException在IabHelper.queryPurchases中

今天我发现了一个涉及以下堆栈跟踪的 Android应用程序的应用程序崩溃报告:
java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getPurchases(int,java.lang.String,java.lang.String)' on a null object reference
at com.myapp.utils.IabHelper.queryPurchases(IabHelper.java:878)
at com.myapp.utils.IabHelper.queryInventory(IabHelper.java:572)
at com.myapp.utils.IabHelper.queryInventory(IabHelper.java:545)
at com.myapp.utils.IabHelper$2.run(IabHelper.java:645)
at java.lang.Thread.run(Thread.java:818)

(行号是从原来的 – 或者什么looks like to be the original,因为自定义重新格式化)

通常,可以修改自己的代码来检查未分配的类成员.问题是这个代码是从Android SDK中复制和粘贴的,因为IabHelper是一个类,Android SDK provides是实现应用内结算v3的好起点

有罪的是第二个

logDebug("Calling getPurchases with continuation token: " + continuetoken);
Bundle ownedItems = mService.getPurchases(3,mContext.getPackageName(),itemType,continuetoken);

看来在调用方法时该服务没有连接. Nexus 5设备上出现此错误(根据开发者控制台)

这是Android 5的已知问题吗?
>是否有最新版本的IAB助手?
>我可以做什么而不是手动编辑代码去处理NPE?

解决方法

修改了这段代码
do {
        logDebug("Calling getPurchases with continuation token: " + continuetoken);
        Bundle ownedItems = mService.getPurchases(3,continuetoken);
        // ...
    }

成为这个…

do {
        logDebug("Calling getPurchases with continuation token: " + continuetoken);
        if (mService == null || mContext == null) {
            logError("Our service and/or our context are null.  Exiting.");
            return IABHELPER_UNKNowN_ERROR;
        }
        Bundle ownedItems = mService.getPurchases(3,continuetoken);
        // ...
    }

这几乎肯定是因为进程异步运行,并且当结果返回时,活动/应用程序(或正在)被关闭.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...