0

I'm modifying In-app purchase sample Trivial Drive, adding some button , but got the error, when clicking the "upgrade_button" that invoke the "onUpgradeAppButtonClicked" method ( I want to go to buy premium version on google play).

The log stacktrace is :

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity  
    at android.view.View$1.onClick(View.java:3854) 
    at android.view.View.performClick(View.java:4469) 
    at android.view.View$PerformClick.run(View.java:18788) 
    at android.os.Handler.handleCallback(Handler.java:808) 
    at android.os.Handler.dispatchMessage(Handler.java:103) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:5349) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at android.view.View$1.onClick(View.java:3849) 
 ... 11 more
Caused by: java.lang.NullPointerException
    at com.lm.rosary.Mysterytoprayfirstbefore.setWaitScreen(Mysterytoprayfirstbefore.java:511)
    at com.lm.rosary.Mysterytoprayfirstbefore.onUpgradeAppButtonClicked(Mysterytoprayfirstbefore.java:364)
... 14 more

My java program to click "upgrade_button" is :

public class Mysterytoprayfirstbefore extends Activity implements IabBroadcastListener, OnClickListener {

Button btnJoyful;
Button btnSorrowful;
Button btnGlorious;
Button btnLuminous;
Button btnAutoselectmystery;
Button btnBacktoMainMenu;
Button upgrade_button;


SharedPreferences prefs;

SharedPreferences.Editor editor;

static final String TAG = "BuyPremium";

// Does the user have the premium upgrade?

boolean mIsPremium = false;


// SKUs for our products: the premium upgrade (non-consumable) and gas (consumable)

static final String SKU_PREMIUM = "xxxxxx001";

// (arbitrary) request code for the purchase flow

static final int RC_REQUEST = 10001;

// The helper object

IabHelper mHelper;

// Provides purchase notification while this app is running

IabBroadcastReceiver mBroadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mysterymenubefore);


    String base64EncodedPublicKey = "xxxxxxxQIDAQAB";

    // Create the helper, passing it our context and the public key to verify signatures with
    Log.d(TAG, "Creating IAB helper.");

    mHelper = new IabHelper(this, base64EncodedPublicKey);

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

    mHelper.enableDebugLogging(false);

    // Start setup. This is asynchronous and the specified listener
    // will be called once setup completes.

    Log.d(TAG, "Starting setup.");

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {

            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {

                return;
            }

            // Have we been disposed of in the meantime? If so, quit.

            if (mHelper == null) return;


            mBroadcastReceiver = new IabBroadcastReceiver(Mysterytoprayfirstbefore.this);
            IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
            registerReceiver(mBroadcastReceiver, broadcastFilter);

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

    btnJoyful = (Button) findViewById(R.id.btnJoyful);
    btnSorrowful = (Button) findViewById(R.id.btnSorrowful);
    btnGlorious = (Button) findViewById(R.id.btnGlorious);
    btnLuminous = (Button) findViewById(R.id.btnLuminous);
    btnAutoselectmystery = Button)findViewById(R.id.btnAutoselectmystery);

    btnBacktoMainMenu = (Button) findViewById(R.id.btnBacktoMainMenu);


    upgrade_button = (Button) findViewById(R.id.upgrade_button);


    btnJoyful.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Launching All products Activity
            Intent i = new Intent(getApplicationContext(), Jfp1.class);
            startActivity(i);
        }
    });

    // view products click event        btnSorrowful.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick (View view){
        // Launching create new product activity
        Intent i = new Intent(getApplicationContext(), Sfp1.class);
        startActivity(i);
    }
});
             btnLuminous.setOnClickListener(new View.OnClickListener()

{

    @Override
    public void onClick (View view){
    // Launching create new product activity
    Intent i = new Intent(getApplicationContext(), Mysterytoprayfirstbefore.class);
    startActivity(i);

}
});
     btnGlorious.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View view){
    Intent i = new Intent(getApplicationContext(), Mysterytoprayfirstbefore.class);
    startActivity(i);
}
});
                  btnAutoselectmystery.setOnClickListener(new View.OnClickListener()

{

    @Override
    public void onClick (View view){
    Intent i = new Intent(getApplicationContext(), Mysterytoprayfirstbefore.class);
    startActivity(i);
}
});
                btnBacktoMainMenu.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View view){

    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(i);
}
});

}

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

        Log.d(TAG, "Query inventory finished.");

        if (mHelper == null) return;


        if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        // Do we have the premium upgrade?

        Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
        mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));

        Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));


        Log.d(TAG, "Initial inventory query finished; enabling main UI.");

    }
};

@Override
public void receivedBroadcast() {
    // Received a broadcast notification that the inventory of items has changed
    Log.d(TAG, "Received broadcast notification. Querying inventory.");

    mHelper.queryInventoryAsync(mGotInventoryListener);
}

// User clicked the "Upgrade to Premium" button.

public void onUpgradeAppButtonClicked(View arg0) {

    Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade.");

    setWaitScreen(true);

    String payload = "";

    mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST,
            mPurchaseFinishedListener, payload);
}


@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)) {

        super.onActivityResult(requestCode, resultCode, data);
    } else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}

/**
 * Verifies the developer payload of a purchase.
 */

boolean verifyDeveloperPayload(Purchase p) {
    String payload = p.getDeveloperPayload();
    return true;
}

// Callback for when a purchase is finished

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {

        Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

        // if we were disposed of in the meantime, quit.

        if (mHelper == null) return;

        if (result.isFailure()) {
            complain("Error purchasing: " + result);
            setWaitScreen(false);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            complain("Error purchasing. Authenticity verification failed.");
            setWaitScreen(false);
            return;
        }

        Log.d(TAG, "Purchase successful.");

        if (purchase.getSku().equals(SKU_PREMIUM)) {

            // bought the premium upgrade!

            Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");

            alert("Thank you for upgrading to premium!");

            mIsPremium = true;

            Intent i = new Intent(getApplicationContext(), Mysterytopray.class);
            startActivity(i);

            setWaitScreen(false);
        }

    }
};


// We're being destroyed. It's important to dispose of the helper here!
@Override
public void onDestroy() {
    super.onDestroy();

    // very important:
    if (mBroadcastReceiver != null) {
        unregisterReceiver(mBroadcastReceiver);
    }

    // very important:
    Log.d(TAG, "Destroying helper.");

    if (mHelper != null) {
        mHelper.dispose();
        mHelper = null;
    }
}

// Enables or disables the "please wait" screen.

void setWaitScreen(boolean set) {
    findViewById(R.id.screen_main).setVisibility(set ? View.GONE : View.VISIBLE);
    findViewById(R.id.screen_wait).setVisibility(set ? View.VISIBLE : View.GONE);
}

void complain(String message) {

    Log.e(TAG, "**** BuyPremium  Error: " + message);

    alert("Error: " + message);
}

void alert(String message) {
    AlertDialog.Builder bld = new AlertDialog.Builder(this);
    bld.setMessage(message);
    bld.setNeutralButton("OK", null);
    Log.d(TAG, "Showing alert dialog: " + message);
    bld.create().show();
}


@Override
public void onClick(DialogInterface arg0, int arg1) {
    TODO Auto -generated method stub
}

}

And the screen for upgrade_button is below xml :

<Button style="@style/BlackText"
android:background="@color/orange"
android:id="@+id/upgrade_button"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:layout_width="fill_parent"
android:onClick="onUpgradeAppButtonClicked"
android:text="@string/button_textbuypremium"/>

Other button , when press/click it is okay, only the "upgrade_button" that issued the IlegalSateException.

Where is the issue?

ChallengeAccepted
  • 1,684
  • 1
  • 16
  • 25
  • please format your code properly because it is very unreadible ... – Kristiyan Varbanov Mar 29 '16 at 09:03
  • At least one of the `View`s you're looking for in the `setWaitScreen()` method doesn't exist in the `mysterymenubefore` layout. – Mike M. Mar 29 '16 at 09:04
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Mike M. Mar 29 '16 at 09:14
  • @Mike M, is it may be this one cause the problem ? Because use View arg0 on the "public void onUpgradeAppButtonClicked(View arg0) ??? – Leonard Mamangkey Mar 30 '16 at 02:18
  • I'm not sure what you mean, but you're calling `findViewById()` twice in the `setWaitScreen()` method, and at least one of those calls is returning null. Make sure the `screen_main` `View` and the `screen_wait` `View` are both in the `mysterymenubefore` layout. – Mike M. Mar 30 '16 at 02:31
  • 2
    @Mike M, you are right , After removing/ commented several "setwaitscreen" statements and void setWaitScreen(boolean set) , the error disappeared. Thank you. – Leonard Mamangkey Mar 31 '16 at 07:54

1 Answers1

0

BY removing / commented on several "setwaitscreen" and "void setWaitScreen(boolean set)", because of the View is not available, error disappeared. Thanks to Mike on guided me to that.