motschel123 motschel123 - 23 days ago 7
Java Question

App is crashing when you press a button

When I press the button the app just crashes (here is the xml file to this)

<Button
android:text="Beginn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/startButton"
android:onClick="getJson"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />


And the code that runs when I press the button.

public void getJson(){
editSummoner =(EditText) findViewById(R.id.editSummoner);
if (editSummoner.getText().length() == 0){
Toast.makeText(getBaseContext(),"Please enter a summoner name",
Toast.LENGTH_LONG).show();
}
else if (editSummoner.getText().length() >= 1) {
Toast.makeText(getBaseContext(),"Summoner name is " + editSummoner.getText().toString(),
Toast.LENGTH_LONG).show();
}
}


Any suggestion are welcome, I looked through all the other code and didn't find any misstake. Also the app works when this part of the code is deleted but then the button doesn't work.

Crash report (Android monitor):

11-15 20:56:20.137 29501-29501/com.example.user.lolstats E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.lolstats, PID: 29501
java.lang.IllegalStateException: Could not find method getJson(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'startButton'
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:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Answer

It is expecting the method getJson(View) with a View as parameter, you defined a method getJson() with no parameters.