I'm trying to write a basic app that counts the button taps. It will compile and load onto my phone but when I tap the button the app immediately crashes. My MainActivity class is standard. The code for my button handler is in another class classed FirstActivity which extends my MainActvity class.
Edit to add: I changed tools:context=".MainActivity" to tools:context=".FirstActivity" in the xml but the app still crashes. I've also updated my logcat to show what happens now.
code:
package com.quiz.sherina.quiz_6;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
/**
* Created by Sherina on 2/18/15.
*/
public class FirstActivity extends MainActivity
{
Button upButton;
TextView countText;
int count = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
upButton = (Button) findViewById(R.id.UP_Button);
countText = (TextView) findViewById(R.id.textView);
}
public void buttonHandler(View v)
{
if (v==upButton) count++;
String str = "" + count;
countText.setText(str);
}
}
Here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".FirstActivity">
<TextView
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:id="@+id/UP_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str1"
android:layout_below="@+id/textView"
android:onClick="buttonHandler"/>
</RelativeLayout>
And here's what happens when I tap the button:
02-23 14:17:36.183 4779-4779/com.quiz.sherina.quiz_6 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.quiz.sherina.quiz_6, PID: 4779
java.lang.IllegalStateException: Could not find a method buttonHandler(View) in the activity class com.quiz.sherina.quiz_6.MainActivity for onClick handler on view class android.widget.Button with id 'UP_Button'
at android.view.View$1.onClick(View.java:3817)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
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:5146)
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:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: buttonHandler [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:857)
at android.view.View$1.onClick(View.java:3810)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
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:5146)
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:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
tools:context=".MainActivity"
Tells that your layout is connected to MainActivity, however your method is found in the derived class called FirstActivity. You should change it to:
tools:context=".FirstActivity"
I figured out why my button causing my app to crash. Similar to what @user3249477 had suggested instead of replacing:
tools:context=" "
I had to go into the manifest file and change:
<activity android:name=".MainActivity"
to:
<activity android:name=".FirstActivity"
You could also add it as a new activity tag below the first one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.