简体   繁体   中英

App crashing. Why?

I am making a Pizza idle clicker game and it keeps crashing when I try to buy something from the shop, I think my app might be crashing because of this code:

public void shopPPC (View view) {
    if(pizza >= shopPPC){
        pizza -= shopPPC;
        ppc *= 2;

        shopPPC  *= 1.5;

        sauceBtn.setText("Sauce (1x PPC) Price: "  + shopPPC);
        ppsAndppcView.setText(pps + " PPS |"+ ppc + " PPC");
        pizzaView.setText(pizza + " Pizzas");

        savePrefs(shopPPCKeyString, shopPPC);
        savePrefs(ppcKeyString, ppc);
        savePrefs(pizzaKeyString, pizza);

    }

    else{
        Toast.makeText(this, "You need more pizzas", Toast.LENGTH_SHORT).show();

    }
}

It says "ShopPPC is never used" and whenever I click the button on the app it crashes. What can I do to fix this? Have I overlooked something? Im fairly new to making apps and android studio, any help is appreciated.

This is what I get in the Android Monitor:

10-23 11:57:49.192 4154-4154/com.example.mariu.pizzaclicker E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.mariu.pizzaclicker, PID: 4154
                                                                              java.lang.IllegalStateException: Could not find method sauceBtn(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'sauceBtn'
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                  at android.view.View.performClick(View.java:5610)
                                                                                  at android.view.View$PerformClick.run(View.java:22265)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

This is where my button(s) are initialized.

 public void initialize(){

    pizzaView = (TextView) findViewById(R.id.pizzaView);
        pizzaView.setText(pizza + " Pizzas");

    ppsAndppcView = (TextView) findViewById(R.id.ppsAndppcView);
        ppsAndppcView.setText(pps + " PPS |"+ ppc + " PPC");

    pizzaBtn = (ImageButton) findViewById(R.id.pizzaBtn);

    sauceBtn = (Button) findViewById(R.id.sauceBtn);
        sauceBtn.setText("Sauce (1x PPC) Price: "  + shopPPC);

    cheeseBtn = (Button) findViewById(R.id.cheeseBtn);
        cheeseBtn.setText("Cheese (1x PPS) Price: "  + shopPPS);

it cannot find the method corresponding to your button maybe u changed the button or delete it and make it again ...write from scratch and this time be careful with the button and its method not to have any conflict! ps. maybe your manifest file is missing something ...app crashing is sometimes from there too!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM