Billing API v3 IabHelper NullPointerException

log*_*ray 52 android in-app-billing

编辑4月15日:在IabHelper中捕获nullpointer似乎已经停止了这个问题.我不再看到被抛出的异常,我将接受这个作为答案.


编辑4/04:稍微深入一点.有try catch块可以处理queryPurchases方法的RemoteExceptions和JSONExceptions,但是没有NullPointerException处理.我将尝试包括NullPointer异常处理,因此IabHelper在尝试querySkuDetails时看起来像这样:

    catch (NullPointerException e) {
        throw new IabException(IABHELPER_UNKNOWN_ERROR, "NullPointer while refreshing inventory.", e);
    }
Run Code Online (Sandbox Code Playgroud)

我刚刚提交了一个错误:

https://code.google.com/p/marketbilling/issues/detail?id=114


编辑3/25: 好吧,仍然收到这个崩溃...现在它发生在尝试从IabHelper的以下摘录的第3行获取上下文时:

int queryPurchases(Inventory inv, String itemType) throws JSONException, RemoteException {
    logDebug("Querying owned items, item type: " + itemType);
    logDebug("Package name: " + mContext.getPackageName());
Run Code Online (Sandbox Code Playgroud)

这很令人沮丧,因为在我的清单中,我总是使用我的应用程序的完整路径名称"name".

示例"com.myappname.blah.ClassName"

我也尝试将这个,MyClass.this,getApplicationContext()传递给mHelper.但是它们都会从野外设备中随机生成相同的NullPointer结果.我还在清单中尝试了name =".MyClass".这就是目前的样子:

mHelper = new IabHelper(MyClass.this, myKey);
Run Code Online (Sandbox Code Playgroud)

编辑 3/18/13:我仍然收到异常,即使在3/17上部署了新的IabHelper版本.

我开始在这里看到一个模式,崩溃是在执行mContext.getPackageName()时尝试获取上下文的全部内容.我很好奇为什么它适用于我的所有测试设备,我无法重现这种崩溃,而且似乎只是在少数设备上.

这是新的崩溃:

java.lang.NullPointerException
    at com.myapp.util.IabHelper.queryPurchases(SourceFile:836)
    at com.myapp.util.IabHelper.queryInventory(SourceFile:558)
    at com.myapp.util.IabHelper.queryInventory(SourceFile:522)
    at com.myapp.util.IabHelper$2.run(SourceFile:617)
    at java.lang.Thread.run(Thread.java:1019)
Run Code Online (Sandbox Code Playgroud)

由IabHelper引起的......

line 836: logDebug("Package name: " + mContext.getPackageName());
Run Code Online (Sandbox Code Playgroud)

编辑 3/17/13:我看到过去几个月发布了很多错误修复,我将尝试这里提供的最新代码,看看这是否能解决问题:

https://code.google.com/p/marketbilling/source/browse/v3/src/com/example/android/trivialdrivesample/util


在我的某个应用中,我使用的是结算API和随附的样板代码.

我正在使用自2013年3月16日起通过SDK管理器提供的最新版本的结算API.

在我的活动中,我使用以下内容查询库存:

final List<String> skuList = new ArrayList<String>();
skuList.add("sku1");
skuList.add("sku2");
skuList.add("sku3");
if (skuList != null) {
    if (skuList.size() > 0) {
        try {
            mHelper.queryInventoryAsync(true, skuList, mGotInventoryListener);
        } catch (Exception e) { 
            ACRA.getErrorReporter().handleException(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在IabHelper类中为以下设备接收了多个NullPointerException报告.我无法重现该问题,也无法找到有关这些崩溃的任何信息,这也是我发布此问题的原因.

我在计费API的"面向开发人员"部分有无数的空值和try/catch块检查,包括在onQueryInventoryFinished中,所以我知道这个异常不是从"我的代码"抛出的(因为我没有捕获崩溃来自我的任何应用程序的类),而是从IabHelper本身抛出.我没有修改IabHelper而不是这个推荐的修复程序:https://stackoverflow.com/a/14737699

崩溃#1 Galaxy Nexus

java.lang.NullPointerException
    at com.myapp.util.IabHelper.querySkuDetails(SourceFile:802)
    at com.myapp.util.IabHelper.queryInventory(SourceFile:471)
    at com.myapp.util.IabHelper$2.run(SourceFile:521)
    at java.lang.Thread.run(Thread.java:856)
Run Code Online (Sandbox Code Playgroud)

由IabHelper引起的......

line 802: Bundle skuDetails = mService.getSkuDetails(3, mContext.getPackageName(), ITEM_TYPE_INAPP, querySkus);    
Run Code Online (Sandbox Code Playgroud)

碰撞#2三星GT-S5570L

java.lang.NullPointerException
    at com.myapp.util.IabHelper.queryPurchases(SourceFile:735)
    at com.myapp.util.IabHelper.queryInventory(SourceFile:465)
    at com.myapp.util.IabHelper$2.run(SourceFile:521)
    at java.lang.Thread.run(Thread.java:1019)
Run Code Online (Sandbox Code Playgroud)

由IabHelper引起的......

line 735: Bundle ownedItems = mService.getPurchases(3, mContext.getPackageName(), ITEM_TYPE_INAPP, continueToken);
Run Code Online (Sandbox Code Playgroud)

log*_*ray 24

编辑4月15日:在IabHelper中捕获nullpointer似乎已经停止了这个问题.我不再看到被抛出的异常,我将接受这个作为答案.


编辑4/04:稍微深入一点.有try catch块可以处理queryPurchases方法的RemoteExceptions和JSONExceptions,但是没有NullPointerException处理.我将尝试包括NullPointer异常处理,因此IabHelper在尝试querySkuDetails时看起来像这样:

    catch (NullPointerException e) {
        throw new IabException(IABHELPER_UNKNOWN_ERROR, "NullPointer while refreshing inventory.", e);
    }
Run Code Online (Sandbox Code Playgroud)

我刚刚提交了一个错误:

https://code.google.com/p/marketbilling/issues/detail?id=114


更改

        if (querySkuDetails) {
            r = querySkuDetails(ITEM_TYPE_INAPP, inv, moreItemSkus);
            if (r != BILLING_RESPONSE_RESULT_OK) {
                throw new IabException(r, "Error refreshing inventory (querying prices of items).");
            }
        }
Run Code Online (Sandbox Code Playgroud)

        if (querySkuDetails) {
            try {
                r = querySkuDetails(ITEM_TYPE_INAPP, inv, moreItemSkus);
                if (r != BILLING_RESPONSE_RESULT_OK) {
                    throw new IabException(r, "Error refreshing inventory (querying prices of items).");
                }
            } catch (NullPointerException e) {
                throw new IabException(IABHELPER_UNKNOWN_ERROR, "NPE while refreshing inventory.", e);
            }
        }
Run Code Online (Sandbox Code Playgroud)

更改

            if (querySkuDetails) {
                r = querySkuDetails(ITEM_TYPE_SUBS, inv, moreSubsSkus);
                if (r != BILLING_RESPONSE_RESULT_OK) {
                    throw new IabException(r, "Error refreshing inventory (querying prices of subscriptions).");
                }
            }
Run Code Online (Sandbox Code Playgroud)

            if (querySkuDetails) {
                try {
                    r = querySkuDetails(ITEM_TYPE_SUBS, inv, moreSubsSkus);
                    if (r != BILLING_RESPONSE_RESULT_OK) {
                        throw new IabException(r, "Error refreshing inventory (querying prices of subscriptions).");
                    }
                } catch (NullPointerException e) {
                    throw new IabException(IABHELPER_UNKNOWN_ERROR, "NPE while refreshing inventory.", e);
                }
            }
Run Code Online (Sandbox Code Playgroud)


tma*_*hey 20

您可能正在使用异步操作.如果您使用...异步方法,当前的IabHelper是不安全的.问题是,在异步操作运行的任何时刻都可以在主线程上调用dispose.在这种情况下,您将获得NullPointerExceptions和IllegalStateExceptions.

这是修补它的补丁:

Index: src/com/evotegra/aCoDriver/iabUtil/IabHelper.java
===================================================================
--- src/com/evotegra/aCoDriver/iabUtil/IabHelper.java   (revision 1162)
+++ src/com/evotegra/aCoDriver/iabUtil/IabHelper.java   (working copy)
@@ -86,7 +86,10 @@

     // Is an asynchronous operation in progress?
     // (only one at a time can be in progress)
-    boolean mAsyncInProgress = false;
+    volatile boolean mAsyncInProgress = false;
+    
+    // is set to true if dispose is called while a thread is running. Allows graceful shutdown
+    volatile boolean mDisposeRequested = false;

     // (for logging/debugging)
     // if mAsyncInProgress == true, what asynchronous operation is in progress?
@@ -285,6 +288,12 @@
      * disposed of, it can't be used again.
      */
     public void dispose() {
+       // do not dispose while an async Thread is running. Will cause all kinds of exceptions.
+       // In this case dispose must be called from thread after setting mAsyncInProgress to true
+       if (mAsyncInProgress) {
+           mDisposeRequested = true;
+           return;
+       }
         logDebug("Disposing.");
         mSetupDone = false;
         if (mServiceConn != null) {
@@ -827,6 +836,7 @@
         logDebug("Ending async operation: " + mAsyncOperation);
         mAsyncOperation = "";
         mAsyncInProgress = false;
+        if (mDisposeRequested) IabHelper.this.dispose();
     }
Run Code Online (Sandbox Code Playgroud)

或者在这里下载补丁. http://code.google.com/p/marketbilling/issues/detail?id=139&thanks=139&ts=1375614409

  • 不幸的是,IabHelper还有其他一些缺陷.一般来说,它不是生产准备好的,imo应该**不能用于任何公共应用程序.我在Google trevorjohns@google.com上通知了代码的所有者.我甚至得到了回复说"开发人员告诉我它已修复当前版本",但实际上并非如此.也许你给他写了一封电子邮件,请他们解决它. (4认同)