应用内结算V3库的“onActivityResult”中的NullPointerException

时间:2018-05-29 23:49:33

标签: android nullpointerexception in-app-purchase in-app-billing

在App Billing V3中通过库完成测试购买后,我的应用程序中出现了一个NullPointerException的小问题。

我可以通过点击特定的menuItem来成功调用测试购买。我的问题是,在测试购买窗口关闭后,而不是获取有关该项目是否已被购买的信息,或者logcat中的任何其他信息,它会抛出NPE错误,我将在下面粘贴:

Error in handleActivityResult
    java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:156)
        at org.json.JSONObject.<init>(JSONObject.java:173)
        at com.anjlab.android.iab.v3.BillingProcessor.handleActivityResult(BillingProcessor.java:932)
        at com.example.android.visitri.MainActivity.onActivityResult(MainActivity.java:806)
        at android.app.Activity.dispatchActivityResult(Activity.java:6931)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4090)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4137)
        at android.app.ActivityThread.-wrap20(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6123)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

我为应用内结算设置的库功能是否正常?我可能会因为我刚刚使用测试购买或者我的设置错误而导致出现此NPE错误吗?

您可以在下面粘贴的代码底部附近找到onActivityResult(错误来源)。

BillingProcessor bp;

---

bp = BillingProcessor.newBillingProcessor(this, null, this);
bp.initialize();

---

if (i == R.id.more_menu) {
            View menuItemView = findViewById(R.id.more_menu);
            PopupMenu popup = new PopupMenu(this, menuItemView);
            // Inflate the menu from xml
            popup.inflate(R.menu.popup_more_menu);
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.menu_remove_ads:
                            boolean isAvailable =
                                    BillingProcessor.isIabServiceAvailable(MainActivity.this);
                            // If the Play Store isn't installed on the phone (if it's a certain
                            // Chinese phone, older phone, or otherwise), don't run the purchase.
                            if (!isAvailable) {
                                return true;
                            }
                            // Checks to see if Billing is supported,
                            // and if it is, run the purchase.
                            boolean isOneTimePurchaseSupported = bp.isOneTimePurchaseSupported();
                            if (isOneTimePurchaseSupported) {
                                bp.purchase(MainActivity.this, "android.test.canceled");
                            }
                            return true;
                    }
                }
            });

---

@Override
    public void onBillingInitialized() {
        /*
         * Called when BillingProcessor was initialized and it's ready to purchase
         */
    }

    @Override
    public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
        /*
         * Called when requested PRODUCT ID was successfully purchased
         */
    }

    @Override
    public void onBillingError(int errorCode, @Nullable Throwable error) {
        /*
         * Called when some error occurred. See Constants class for more details
         *
         * Note - this includes handling the case where the user canceled the buy dialog:
         * errorCode = Constants.BILLING_RESPONSE_RESULT_USER_CANCELED
         */
    }

    @Override
    public void onPurchaseHistoryRestored() {
        /*
         * Called when purchase history was restored and the list of all owned PRODUCT ID's
         * was loaded from Google Play
         */
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!bp.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onDestroy() {
        if (bp != null) {
            bp.release();
        }
        super.onDestroy();
    }

非常感谢您对我的任何建议或意见! :)

0 个答案:

没有答案