android - 应用内结算:无法设置 IAB Helper 异常(exception)

标签 android in-app-billing

我正在尝试在 Google Play 中使用静态产品 ID 设置应用内结算和测试。 我正在关注开发者教程 here . 当在 labHelper 对象上调用 launhPurcahseFlow 方法时,出现异常:

java.lang.IllegalStateException: IAB helper is not set up. Can't perform operation: launchPurchaseFlow at com.android.vending.billing.IabHelper.checkSetupDone(IabHelper.java:782)

已经搜索了几个小时,但找不到有效的解决方案。 任何意见表示赞赏。

我的代码是:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


           // compute your public key and store it in base64EncodedPublicKey
           mHelper = new IabHelper(this, base64EncodedPublicKey);

        // enable debug logging (for a production application, you should set this to false).
            mHelper.enableDebugLogging(true);


           //perform service binding to Google Bill ser and return ny errors with IabResult object and listener

            mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                public void onIabSetupFinished(IabResult result) {
                    Log.d(TAG, "Setup finished.");

                    if (!result.isSuccess()) {
                        // Oh noes, there was a problem.
                        alert("Problem setting up in-app billing: " + result);
                        return;
                    }

                    // Have we been disposed of in the meantime? If so, quit.
                    if (mHelper == null) return;

                    // IAB is fully set up. Now, let's get an inventory of stuff we own.
                    Log.d(TAG, "Setup successful. Querying inventory.");
                    //mHelper.queryInventoryAsync(mGotInventoryListener);
                }
            });


           //ILLEGALSTAEEXCEPTION THROWN HERE

           mHelper.launchPurchaseFlow(this, testProduct, RC_REQUEST,   
                   new IabHelper.OnIabPurchaseFinishedListener() {
                       public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
                       {
                          if (result.isFailure()) {
                             Log.d(TAG, "Error purchasing: " + result);
                             return;
                          } 
                          else if (purchase.getSku().equals(testProduct)) {
                             // give user access to premium content and update the UI
                              //set the purchaesd booean to true


                            //when purcajsed add this code
                                editor.putBoolean("purchased", true);
                                editor.commit();
                                Toast.makeText(getApplicationContext(), "ADD FREE VERSION PURCAHSED!!!" +
                                        " Details OrderID: "+purchase.getOrderId() +" Payload ID: "+purchase.mDeveloperPayload, Toast.LENGTH_LONG).show();
                                Log.d("CONNECT TO GOOGLE BILL", "ITEM PURCAHSED! : "+purchased);
                          }
                       }
                    }, "diveAppPurchase");





    }//onCreate
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
            if (mHelper == null) return;

            // 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.");
            }
        }

最佳答案

这对像我这样第一次处理应用内购买的人很有用。

设置调用是异步的,因此必须在调用 launchPurchaseFlow 之前完成。所以我禁用了一个“购买”按钮来进行 launchPurchaseFlow 调用,一旦设置调用完成,它就会被启用。工作正常:

设置调用 //执行与 Google Bill ser 的服务绑定(bind)并使用 IabResult 对象和监听器返回 ny 错误

        mHelper.startSetup(...)

如果成功则启用按钮并调用 mHelper.launchPurchaseFlow(...) 方法

此外,当使用 google 提供的测试产品 ID 时,您可能会注意到在 launchPurchaseFlow 期间抛出签名异常,尽管交易成功,但随后的结果失败,这显然是 google 知道的已知错误

关于android - 应用内结算:无法设置 IAB Helper 异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24018099/

相关文章:

android - 将自定义JAR库添加到Android项目时,Delphi10.2中出现编译器错误

java - 如何通过 PHP 连接 Android 应用和 MySQL 数据库

android - 是否可以在 native react 中开发移动键盘应用程序

android - 如何在inapp v3 android中获取订阅到期日期

android - 使用背景图像时图库滚动性能不佳

java - 如何在 ImageView Android 上添加标记/图钉?

android - 控制台中的 BILLING 权限错误,尽管它不在 list 中

android - Android 中的应用内购买

android - onPurchaseStateChange 没有被调用

java - IabHelper.startSetup() 导致 NullPointerException