Reputation: 2195
I have just started learning android. I am getting this error.Please help me fix this.
This is my .xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sumit.myapplication.MainActivity"
tools:ignore="ExtraText">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="43dp"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat" />
<Button
android:text="Sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="97dp"
android:id="@+id/button"
android:elevation="10dp"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:onClick="onButtonClick (MainActivity)" />
</RelativeLayout>
And this is my .java file
package com.example.sumit.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
{
TextView t = (TextView)findViewById(R.id.textView);
t.setText("sumit");
}
}
And I am getting this error.
E/EGL_emulation: tid 3738: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH) W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f0d46d90c40, error=EGL_BAD_MATCH D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sumit.myapplication, PID: 3720 java.lang.IllegalStateException: Could not find method onButtonClick (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button' 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:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) I/Process: Sending signal. PID: 3720 SIG: 9 Application terminated.
How do I fix this ? Please help.
Upvotes: 1
Views: 2147
Reputation: 4122
Well. your stacktrace
explains it:
Could not find method onButtonClick (MainActivity)
There's no method with that name. the name shouldn't contain the (MainActivity)
part.
in the .xml, write this:
<Button
android:text="Sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="97dp"
android:id="@+id/button"
android:elevation="10dp"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:onClick="onButtonClick" />
Upvotes: 1
Reputation: 955
instead of
android:onClick="onButtonClick (MainActivity)"
replace with :
android:onClick="onButtonClick"
Note that with the XML above, Android will look for the onClick method onButtonClick() only in the current Activity.
Upvotes: 1
Reputation: 1872
use
android:onClick="onButtonClick"
instead of
android:onClick="onButtonClick (MainActivity)"
View parameter is passed by android.
Upvotes: 1