-3
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.

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();
    }
}
Julian
  • 19
  • 1
  • 8
  • 3
    you have the wrong signature for `aldirindersalami`. It should be `public void aldirindersalami(View view)` – Blackbelt Nov 02 '16 at 12:05
  • What @Blackbelt explains come simply from the 3 line (in general this is the one important but simply need to read the error) `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` Following, a duplcate with a nice explanation – AxelH Nov 02 '16 at 12:07

1 Answers1

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

public void aldirindersalami(View v) {}

to handle the click on the button.

Alex
  • 3,382
  • 2
  • 32
  • 41