This question already has an answer here:
这个问题在这里已有答案:
- How exactly does the android:onClick XML attribute differ from setOnClickListener? 16 answers
- android:onClick XML属性与setOnClickListener有何不同? 16个答案
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.juliandrach.eatfit, PID: 2223
java.lang.IllegalStateException: Could not find method aldirindersalami(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
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:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Basically the error occurs when I am updating a variable by pressing a button and then trying to display the value of the variable with a toast in the same task.
基本上,当我通过按下按钮更新变量然后尝试在同一任务中显示带有Toast的变量的值时,会发生错误。
public class Mahlzeiten extends AppCompatActivity {
public static int proteine = 0;
public static int carbs = 0;
public static int fette = 0;
public static int kcal = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mahlzeiten);
String text = "Hallo" + proteine + "danke";
Toast.makeText(Mahlzeiten.this, text, Toast.LENGTH_SHORT).show();
}
public void aldirindersalami (int proteine){
proteine++;
// String text1 = "Hallo" + proteine + "danke";
// Toast.makeText(Mahlzeiten.this, text1, Toast.LENGTH_SHORT).show();
}
}
1 个解决方案
#1
2
You have an onClick listener set in the xml but you didn't implement the onClick method in the code. You need the following method
您在xml中设置了onClick侦听器,但未在代码中实现onClick方法。您需要以下方法
public void aldirindersalami(View v) {}
to handle the click on the button.
处理按钮上的点击。
#1
2
You have an onClick listener set in the xml but you didn't implement the onClick method in the code. You need the following method
您在xml中设置了onClick侦听器,但未在代码中实现onClick方法。您需要以下方法
public void aldirindersalami(View v) {}
to handle the click on the button.
处理按钮上的点击。