IAB助手不成立。无法执行操作:queryInventory不成立、助手、操作、IAB

2023-09-06 06:58:36 作者:只有眼泪是真的。

我试图实施应用计费API在我的应用程序,但是当我跑我的应用程序它trows异常。这是我在应用计费API处理这样的建议将是非常美联社preciated第一次。这是我得到异常的日志:

 流程:koemdzhiev.com.quickshoppinglist,PID:10604了java.lang.RuntimeException:无法启动活动ComponentInfo {koemdzhiev.com.quickshoppinglist / koemdzhiev.com.quickshoppinglist.ui.MainActivity}:java.lang.IllegalStateException:IAB帮手不成立。无法执行操作:queryInventory        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)        在android.app.ActivityThread.access $ 900(ActivityThread.java:147)        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1296)        在android.os.Handler.dispatchMessage(Handler.java:102)        在android.os.Looper.loop(Looper.java:135)        在android.app.ActivityThread.main(ActivityThread.java:5254)        在java.lang.reflect.Method.invoke(本机方法)        在java.lang.reflect.Method.invoke(Method.java:372)        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:898)        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) java.lang.IllegalStateException:引起IAB助手不成立。无法执行操作:queryInventory        在koemdzhiev.com.quickshoppinglist.utils.IabHelper.checkSetupDone(IabHelper.java:782)        在koemdzhiev.com.quickshoppinglist.utils.IabHelper.queryInventoryAsync(IabHelper.java:610)        在koemdzhiev.com.quickshoppinglist.utils.IabHelper.queryInventoryAsync(IabHelper.java:639)        在koemdzhiev.com.quickshoppinglist.ui.MainActivity.queryPurchasedItems(MainActivity.java:187)        在koemdzhiev.com.quickshoppinglist.ui.MainActivity.onStart(MainActivity.java:193)        在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220) 

我的code的主要活动:

 公共类MainActivity扩展AppCompatActivity {私有静态最后弦乐TAG = MainActivity.class.getSimpleName();私人工具栏mToolbar;私人RecyclerView mRecyclerView;私人的ArrayList<串GT; shoppingListItems;私人共享preferences mShared preferences;私人共享preferences.Editor mEditor;私人TextView的mEmptyTextView;私人ShoppingListAdapter适配器;私人ActionButton actionButton;私人MaterialDialog addItemdialog = NULL;私人的AdView mAdView;私人IabHelper mHelper;私人字符串SKU_REMOVE_ADDS =remove_adds_sku;私人布尔mIsRemoveAdds = FALSE;私人IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener =新IabHelper.OnIabPurchaseFinishedListener(){    @覆盖    公共无效onIabPurchaseFinished(IabResult结果,购购){        如果(result.isFailure()){            Log.d(TAG,错误的采购:+结果);            返回;        }        否则如果(purchase.getSku()。等于(SKU_REMOVE_ADDS)){            //消耗气体和更新UI            mIsRemoveAdds = TRUE;            mAdView.setVisibility(View.GONE);            Toast.makeText(MainActivity.this,购买成功,Toast.LENGTH_LONG).show();        }    }};@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);串公钥= S1 + S2 + S3 + S4 + S5;    mHelper =新IabHelper(这一点,公钥);    mHelper.startSetup(新IabHelper.OnIabSetupFinishedListener(){        @覆盖        公共无效onIabSetupFinished(IabResult结果){            如果(!result.isSuccess()){                //错误                Log.d(TAGProglem建立在应用内结算:+结果);            }            // Horay,IAB是完全成立!            Log.d(TAG,Horay,IAB是完全成立!);            queryPurchasedItems();        }    });私人无效queryPurchasedItems(){    //检查用户是否已购买删除补充道    IabHelper.QueryInventoryFinishedListener mGotInventoryListener =新IabHelper.QueryInventoryFinishedListener(){        @覆盖        公共无效onQueryInventoryFinished(IabResult结果,库存盘点){            如果(result.isFailure()){                //这里处理错误                Toast.makeText(MainActivity.this,错误,Toast.LENGTH_LONG).show();            }            其他{                //用户是否具有premium升级?                mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);                如果(!mIsRemoveAdds){                    Toast.makeText(MainActivity.this,没有premium,Toast.LENGTH_LONG).show();                    mAdView =(的AdView)findViewById(R.id.adView);                    AdRequest中AdRequest中=新AdRequest.Builder()建立()。                    mAdView.loadAd(AdRequest中);                }其他{                    mAdView.setVisibility(View.GONE);                    Toast.makeText(MainActivity.this,premium,Toast.LENGTH_LONG).show();                }            }        }    };    mHelper.queryInventoryAsync(mGotInventoryListener);}@覆盖保护无效调用onStart(){    super.onStart();    queryPurchasedItems();    isListEmpty();}@覆盖保护无效onResume(){    super.onResume();    queryPurchasedItems();    isListEmpty();}@覆盖保护无效的onDestroy(){    super.onDestroy();    如果(mHelper!= NULL)mHelper.dispose();    mHelper = NULL;    mAdView.destroy();} 

解决方案

最后几个小时的尝试和错误,尝试许多建议的解决方案,我得到了整个事情的工作而不是得到这个异​​常IAB帮手未设置后。无法执行操作:queryInventory或者一个以上是我得到无法启动异步操作(消费),因为另一个异步操作(消费)正在进行中。你必须做的第一件事是添加在IabHelper.java类2种方法:

 公共布尔isAsyncInProgress(){    返回mAsyncInProgress;}公共布尔isSetupDone(){    返回mSetupDone;} 
如何用一个excel文件建立microsoft query数据源

和比你的主要活动:

 公共类MainActivity扩展AppCompatActivity {私人IabHelper.QueryInventoryFinishedListener mGotInventoryListener =新IabHelper.QueryInventoryFinishedListener(){    @覆盖    公共无效onQueryInventoryFinished(IabResult结果,库存盘点){        如果(result.isFailure()){            //这里处理错误            Toast.makeText(MainActivity.this,错误,Toast.LENGTH_LONG).show();        }        其他{            //用户是否具有premium升级?            mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);            如果(!mIsRemoveAdds){                Toast.makeText(MainActivity.this,没有premium,Toast.LENGTH_LONG).show();                mAdView =(的AdView)findViewById(R.id.adView);                AdRequest中AdRequest中=新AdRequest.Builder()建立()。                mAdView.loadAd(AdRequest中);            }其他{                mAdView.setVisibility(View.GONE);                Toast.makeText(MainActivity.this,premium,Toast.LENGTH_LONG).show();            }        }    }};私人IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener =新IabHelper.OnIabPurchaseFinishedListener(){    @覆盖    公共无效onIabPurchaseFinished(IabResult结果,购购){        如果(result.isFailure()){            Log.d(TAG,错误的采购:+结果);            返回;        }        否则如果(purchase.getSku()。等于(SKU_REMOVE_ADDS)){            //消耗气体和更新UI            mIsRemoveAdds = TRUE;            mAdView.setVisibility(View.GONE);            Toast.makeText(MainActivity.this,购买成功,Toast.LENGTH_LONG).show();        }    }}; @覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);串公钥= S1 + S2 + S3 + S4 + S5;    mHelper =新IabHelper(这一点,公钥);    mHelper.startSetup(新IabHelper.OnIabSetupFinishedListener(){        @覆盖        公共无效onIabSetupFinished(IabResult结果){            如果(!result.isSuccess()){                //错误                Log.d(TAGProglem建立在应用内结算:+结果);            }            如果(result.isSuccess()){                // Horay,IAB是完全成立!                Log.d(TAG,Horay,IAB是完全成立!);                // queryPurchasedItems;                mHelper.queryInventoryAsync(mGotInventoryListener);            }        }    }); 

因为它是在谷歌的指南推荐

而现在, 了解我们要检查用户已经购买了哪些项目在启动或恢复方法API。在这里,我们会需要2种方法,我们在IabHelper.java类添加。​​

 私人无效queryPurchasedItems(){    //检查用户是否已购买删除补充道    如果(mHelper.isSetupDone()及&放大器;!mHelper.isAsyncInProgress()){        mHelper.queryInventoryAsync(mGotInventoryListener);    }}@覆盖保护无效调用onStart(){    super.onStart();    queryPurchasedItems();}@覆盖保护无效onResume(){    super.onResume();    queryPurchasedItems();    isListEmpty();} 

I am trying to implement in-app-billing API in my app but when I run my app it trows an exception. This is the first time that I am dealing with in-app-billing API so suggestions would be highly appreciated. This is the log of the exception that I am getting:

Process: koemdzhiev.com.quickshoppinglist, PID: 10604
java.lang.RuntimeException: Unable to start activity ComponentInfo{koemdzhiev.com.quickshoppinglist/koemdzhiev.com.quickshoppinglist.ui.MainActivity}: java.lang.IllegalStateException: IAB helper is not set up. Can't perform operation: queryInventory
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
        at android.app.ActivityThread.access$900(ActivityThread.java:147)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
 Caused by: java.lang.IllegalStateException: IAB helper is not set up. Can't perform operation: queryInventory
        at koemdzhiev.com.quickshoppinglist.utils.IabHelper.checkSetupDone(IabHelper.java:782)
        at koemdzhiev.com.quickshoppinglist.utils.IabHelper.queryInventoryAsync(IabHelper.java:610)
        at koemdzhiev.com.quickshoppinglist.utils.IabHelper.queryInventoryAsync(IabHelper.java:639)
        at koemdzhiev.com.quickshoppinglist.ui.MainActivity.queryPurchasedItems(MainActivity.java:187)
        at koemdzhiev.com.quickshoppinglist.ui.MainActivity.onStart(MainActivity.java:193)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)

My code in main activity:

public class MainActivity extends AppCompatActivity {
private static final String TAG =  MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private RecyclerView mRecyclerView;
private ArrayList<String> shoppingListItems;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private TextView mEmptyTextView;
private ShoppingListAdapter adapter;
private ActionButton actionButton;
private MaterialDialog addItemdialog = null;
private AdView mAdView;
private IabHelper mHelper;
private String SKU_REMOVE_ADDS = "remove_adds_sku";
private boolean mIsRemoveAdds = false;
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
            // consume the gas and update the UI
            mIsRemoveAdds = true;
            mAdView.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
String publicKey = s1+s2+s3+s4+s5;

    mHelper = new IabHelper(this,publicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                //error
                Log.d(TAG, "Proglem setting up in-app Billing: " + result);
            }
            //Horay, IAB is fully set up!
            Log.d(TAG, "Horay, IAB is fully set up!");
            queryPurchasedItems();
        }
    });
private void queryPurchasedItems() {
    //check if user has bought "remove adds"
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                // handle error here
                Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
            }
            else{
                // does the user have the premium upgrade?
                mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
                if(!mIsRemoveAdds) {
                    Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                    mAdView = (AdView) findViewById(R.id.adView);
                    AdRequest adRequest = new AdRequest.Builder().build();
                    mAdView.loadAd(adRequest);
                }else{
                    mAdView.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
                }

            }
        }
    };
    mHelper.queryInventoryAsync(mGotInventoryListener);
}
@Override
protected void onStart() {
    super.onStart();
    queryPurchasedItems();
    isListEmpty();
}

@Override
protected void onResume() {
    super.onResume();
    queryPurchasedItems();
    isListEmpty();
}

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

解决方案

Finally after many hours of try and error, trying many suggested solutions I got the whole thing to work and not got this exception "IAB helper is not set up. Can't perform operation: queryInventory" or one more that I was getting "Can't start async operation (consume) because another async operation(consume) is in progress". The first thing that you have to do is to add 2 methods in the IabHelper.java class:

 public boolean isAsyncInProgress(){
    return mAsyncInProgress;
}
public boolean isSetupDone (){
    return mSetupDone;
}

and than on your main activity:

public class MainActivity extends AppCompatActivity {
private   IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            // handle error here
            Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
        }
        else{
            // does the user have the premium upgrade?
            mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
            if(!mIsRemoveAdds) {
                Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }else{
                mAdView.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
            }

        }
    }
};
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
            // consume the gas and update the UI
            mIsRemoveAdds = true;
            mAdView.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
String publicKey = s1+s2+s3+s4+s5;

    mHelper = new IabHelper(this,publicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                //error
                Log.d(TAG, "Proglem setting up in-app Billing: " + result);
            }
            if (result.isSuccess()) {
                //Horay, IAB is fully set up!
                Log.d(TAG, "Horay, IAB is fully set up!");
                //queryPurchasedItems;
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        }
    });

And now, as it is recommended in Google's guide for the API we have to check what items the user has bought in on start or on resume methods. Here we gonna need the 2 methods that we added in IabHelper.java class.

   private void queryPurchasedItems() {
    //check if user has bought "remove adds"
    if(mHelper.isSetupDone() && !mHelper.isAsyncInProgress()) {
        mHelper.queryInventoryAsync(mGotInventoryListener);
    }
}

@Override
protected void onStart() {
    super.onStart();
    queryPurchasedItems();
}

@Override
protected void onResume() {
    super.onResume();
    queryPurchasedItems();
    isListEmpty();
}