这个错误消息是什么意思?

时间:2016-11-02 12:04:02

标签: java android xml variables

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)

基本上,当我通过按一个按钮更新变量然后尝试在同一任务中用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 个答案:

答案 0 :(得分:2)

您在xml中设置了onClick侦听器,但未在代码中实现onClick方法。您需要以下方法

public void aldirindersalami(View v) {}

处理点击按钮。