为了在适用于 Android 的 app billing v3 中进行测试,我实现了一个对话框,该对话框添加了一个单选项适配器。我添加了测试 Sku,“android.test.purchased”、“android.test.canceled”、“android.test.refunded”、“android.test.item_unavailable”。
当我使用对话框启动购买流程时,一切都很好,我可以购买该物品并且该物品绝对是购买的。但是,当我的应用程序尝试刷新 UI 时,我得到一个空指针异常。我知道刷新工作。
这是我的日志:
12-16 01:06:06.706: I/Running inventory query with(20846): android.test.purchased
12-16 01:06:06.706: I/Running details query with(20846): SkuDetails:{"title":"Sample Title","price":"£0.61","type":"inapp","description":"Sample description for product: android.test.purchased.","productId":"android.test.purchased"}
12-16 01:06:06.706: I/Running details query with(20846): SkuDetails:{"title":"Sample Title","price":"£0.61","type":"inapp","description":"Sample description for product: android.test.purchased.","productId":"android.test.purchased"}
12-16 01:06:06.706: I/Running inventory query with(20846): android.test.canceled
12-16 01:06:06.706: I/Running details query with(20846): null
12-16 01:06:06.706: I/Running details query with(20846): null
12-16 01:06:06.706: D/AndroidRuntime(20846): Shutting down VM
12-16 01:06:06.706: W/dalvikvm(20846): threadid=1: thread exiting with uncaught exception (group=0x40d12930)
12-16 01:06:06.706: E/AndroidRuntime(20846): FATAL EXCEPTION: main
12-16 01:06:06.706: E/AndroidRuntime(20846): java.lang.NullPointerException
12-16 01:06:06.706: E/AndroidRuntime(20846): at com.example.test.MainActivity$1.onQueryInventoryFinished(MainActivity.java:201)
12-16 01:06:06.706: E/AndroidRuntime(20846): at com.example.test.util.IabHelper$2$1.run(IabHelper.java:536)
12-16 01:06:06.706: E/AndroidRuntime(20846): at android.os.Handler.handleCallback(Handler.java:725)
12-16 01:06:06.706: E/AndroidRuntime(20846): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:06:06.706: E/AndroidRuntime(20846): at android.os.Looper.loop(Looper.java:137)
12-16 01:06:06.706: E/AndroidRuntime(20846): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-16 01:06:06.706: E/AndroidRuntime(20846): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:06:06.706: E/AndroidRuntime(20846): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 01:06:06.706: E/AndroidRuntime(20846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-16 01:06:06.706: E/AndroidRuntime(20846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-16 01:06:06.706: E/AndroidRuntime(20846): at dalvik.system.NativeStart.main(Native Method)
for (String s : allSkus) {
Log.i("Running inventory query with", "" + s);
SkuDetails c = inv.getSkuDetails(s);
Log.i("Running details query with", "" + c);
if (inv.hasPurchase(s)) {
purchaseArray.add(c.getTitle());
} else {
**LINE 201** objects.add(c.getPrice() + " T:" + c.getSku());
}
allSkus 是一个包含上述 4 个 skus 的 ArrayList。
空指针是 SkuDetails c = inv.getSkuDetails(s);
在 UI 刷新时返回 null。但问题是它似乎适用于第一个 sku (android.test.purchased) 但对于第二个 sku (android.test.canceled) 是 null 并且它也只有在购买另一个项目后运行时才为 null (退款或购买)。但是,如果我随后再次打开它,那很好,并且不会引发 nullpointerexception。
如果我在添加到 ArrayList 对象之前添加了一个空检查,那么不会添加任何项目,因为在购买的项目之后或之前的每个项目都在 inv.getSkuDetails(s) 上返回空值