Question

I am trying to set up in-app-billing and test using a static product ID in Google Play. Am following developer tutorial here. When the launhPurcahseFlow method is called on the labHelper object I get the exception:

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

Have been searching for hours and can't find a solution that works. Any input appreciated.

My code is:

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.");
            }
        }
Was it helpful?

Solution

This be useful to those like me, who are dealing with in app purchasing first time.

The set up call is asynchronous, therefore this must complete before the launchPurchaseFlow is called. So i disabled a 'Purchase' button to make the launchPurchaseFlow call, which is enabled once the set up call is done. Works fine:

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

        mHelper.startSetup(...)

if successfully then enable button and call mHelper.launchPurchaseFlow(...) method

Also, when using the test product id provided by google you may note a signature exception is thrown during the launchPurchaseFlow with a subsequent result failure although the transaction is successful , this is apparently a known bug that google are aware of

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top