[英]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:我正在制作一个 Pizza 空闲点击器游戏,当我尝试从商店购买东西时它一直崩溃,我认为我的应用程序可能因为以下代码而崩溃:
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.它说“从不使用 ShopPPC”,每当我单击应用程序上的按钮时,它就会崩溃。 What can I do to fix this?我能做些什么来解决这个问题? Have I overlooked something?我是否忽略了什么? Im fairly new to making apps and android studio, any help is appreciated.我对制作应用程序和 android studio 还很陌生,感谢任何帮助。
This is what I get in the Android Monitor:这是我在 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!也许您的清单文件丢失了一些东西……应用程序崩溃有时也是从那里开始的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.