Android应用内结算:" IABHelper未设置"

时间:2015-09-06 17:10:42

标签: java android eclipse in-app-billing

我有一个非常简单的Java for Android应用程序,它实现了应用内结算。它几乎只是从here复制而来。它工作正常。我制作了一份文件,然后开始玩它。我搞砸了(当然),所以我恢复了我的备份文件。现在它还没有运行。这是我的LogCat:

09-06 12:53:09.788: W/ActivityThread(4437): Application 

com.myknitcards.project is waiting for the debugger on port 8100...
09-06 12:53:09.797: I/System.out(4437): Sending WAIT chunk
09-06 12:53:09.980: I/art(4437): Debugger is active
09-06 12:53:09.998: I/System.out(4437): Debugger has connected
09-06 12:53:09.998: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:10.199: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:10.399: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:10.599: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:10.799: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:10.999: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:11.200: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:11.400: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:11.600: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:11.800: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:12.001: I/System.out(4437): waiting for debugger to settle...
09-06 12:53:12.201: I/System.out(4437): debugger has settled (1410)
09-06 12:54:56.727: E/IabHelper(4437): In-app billing error: Illegal state for operation (queryInventory): IAB helper is not set up.
09-06 12:55:35.534: D/AndroidRuntime(4437): Shutting down VM
09-06 12:55:35.539: E/AndroidRuntime(4437): FATAL EXCEPTION: main
09-06 12:55:35.539: E/AndroidRuntime(4437): Process: com.myknitcards.project, PID: 4437
09-06 12:55:35.539: E/AndroidRuntime(4437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myknitcards.project/com.myknitcards.project.MainActivity}: java.lang.IllegalStateException: IAB helper is not set up. Can't perform operation: queryInventory
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.os.Looper.loop(Looper.java:135)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.ActivityThread.main(ActivityThread.java:5254)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at java.lang.reflect.Method.invoke(Native Method)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at java.lang.reflect.Method.invoke(Method.java:372)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-06 12:55:35.539: E/AndroidRuntime(4437): Caused by: java.lang.IllegalStateException: IAB helper is not set up. Can't perform operation: queryInventory
09-06 12:55:35.539: E/AndroidRuntime(4437):     at com.myknitcards.project.util.IabHelper.checkSetupDone(IabHelper.java:782)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at com.myknitcards.project.util.IabHelper.queryInventoryAsync(IabHelper.java:610)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at com.myknitcards.project.MainActivity.onStart(MainActivity.java:85)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.Activity.performStart(Activity.java:6006)
09-06 12:55:35.539: E/AndroidRuntime(4437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
09-06 12:55:35.539: E/AndroidRuntime(4437):     ... 10 more
09-06 12:55:39.033: I/Process(4437): Sending signal. PID: 4437 SIG: 9

这里是代码(虽然记住这个代码一次一次,所以我不认为代码是问题):

package com.myknitcards.project;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.myknitcards.project.util.IabHelper;
import com.myknitcards.project.util.IabResult;
import com.myknitcards.project.util.Inventory;
import com.myknitcards.project.util.Purchase;


public class MainActivity extends Activity {

    private static final String TAG = "com.myknitcards.project";
    IabHelper mHelper;
    static final String ITEM_SKU = "android.test.purchased";
//  static final String ITEM_SKU = "com.example.buttonclick";

    private Button clickButton;
    private Button buyButton;

    @SuppressWarnings("unchecked")
        @Override
    protected void onStart() {
      super.onStart();
      setContentView(R.layout.activity_main); 

        clickButton = (Button)findViewById(R.id.clickButton);
        buyButton = (Button)findViewById(R.id.buyButton);
        clickButton.setEnabled(false);

        String base64EncodedPublicKey = 
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs6G8Ey4Dv1yoJVELLZYjErrpiJxdDYnCIftHHwgv5pNl0ShKAqc5L6fGewVc3phj5rpTMlou+TPTjMldguG6DcjiI5snmqZHoJ5RnjbT4lAUMyH48YwAAMOf1U+0bOXDL2VpnfPS8KXYTGrUl90b5U4rRGrHTQ8HinVPTwjeLkD1EcGSUAfW5FfgN7uqKzX5WAk+wB27MiqBu4cuP7qWFxc4gzuEi/W8v8EjhJRHeip8UJPIfEV9pOfwDQJVby6K+fsZnXHDgfbyF/uJ2sfRrKPt5j4Ln8LbgQ+SYBqd6ccuqsOxdgNICK99+5FjLazm58K1lne363Ri5j+bYyTlewIDAQAB";

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new 
                IabHelper.OnIabSetupFinishedListener() {
                     public void onIabSetupFinished(IabResult result) 
                 {
                           if (!result.isSuccess()) {
                             Log.d(TAG, "In-app Billing setup failed: " + 
                        result);
                           } else {             
                             Log.d(TAG, "In-app Billing is set up OK");
                       }
                         }
                });

        final String i001 = "i001";
        final String i002 = "i002";
            ArrayList<String> additionalSkuList = new ArrayList<String>();
            additionalSkuList.add(i001);
            additionalSkuList.add(i002);

            IabHelper.QueryInventoryFinishedListener mQueryFinishedListener =
                    new IabHelper.QueryInventoryFinishedListener() {

                        @Override
                        public void onQueryInventoryFinished(IabResult result, Inventory inv) {
                            if (result.isFailure()){

                            }

                            String Intermediate1 = 
                                    inv.getSkuDetails(i001).getPrice();
                            String Intermediate2 = 
                                    inv.getSkuDetails(i002).getPrice();
                        }
                    };

            mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
    }

    public void buttonClicked(View view) {
        clickButton.setEnabled(false);
        buyButton.setEnabled(true);
    }

    public void buyClick(View view) {
         mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,   
               mPurchaseFinishedListener, "mypurchasetoken");
    }

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

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
    = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, 
                      Purchase purchase) 
    {
       if (result.isFailure()) {
          // Handle error
          return;
     }      
     else if (purchase.getSku().equals(ITEM_SKU)) {
         consumeItem();
        buyButton.setEnabled(false);
    }

     }
  };

  public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
  }

  IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener 
     = new IabHelper.QueryInventoryFinishedListener() {
       public void onQueryInventoryFinished(IabResult result,
          Inventory inventory) {


          if (result.isFailure()) {
          // Handle failure
          } else {
                   mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), 
            mConsumeFinishedListener);
          }
      }
  };

  IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
      new IabHelper.OnConsumeFinishedListener() {
       public void onConsumeFinished(Purchase purchase, 
               IabResult result) {

     if (result.isSuccess()) {               
          clickButton.setEnabled(true);
     } else {
             // handle error
     }
    }
  };

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

当我调试时,在onStart()的mHelper.startSetup行中抛出LogCat中的第一个错误。我在Mac上用Eclipse工作,只是fyi。唯一改变的是我在SDK Manager和现在工作之间更新了很多东西。这会导致这种情况吗?

1 个答案:

答案 0 :(得分:0)

这是因为mHelper.queryInventoryAsync()类尚未初始化到查询库存时。尝试在onIabSetupFinished()回调中移动chromium-browser --disable-web-security 。这将确保您在初始化完成后调用它。