Android应用程序在我的活动中找不到我的按钮处理程序的方法,导致应用程序在手机上崩溃

时间:2015-02-23 18:06:37

标签: java android

我正在尝试编写一个计算按钮水龙头的基本应用程序。它会编译并加载到我的手机上但是当我点击按钮时,应用程序会立即崩溃。我的MainActivity类是标准的。我的按钮处理程序的代码在另一个类FirstActivity中,它扩展了我的MainActvity类。

编辑添加:我更改了工具:context =" .MainActivity"工具:context =" .FirstActivity"在xml但应用程序仍然崩溃。我还更新了我的logcat以显示现在发生的事情。

代码:

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);
    }
}

这是我的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>

点击按钮后会发生什么:

    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)

2 个答案:

答案 0 :(得分:1)

tools:context=".MainActivity"

告诉您的布局已连接到MainActivity,但您的方法位于名为FirstActivity的派生类中。你应该把它改成:

tools:context=".FirstActivity"

答案 1 :(得分:0)

我弄清楚为什么我的按钮导致我的应用崩溃。类似于@ user3249477建议而不是替换:

tools:context=" "

我必须进入清单文件并更改:

<activity android:name=".MainActivity"

到:

<activity android:name=".FirstActivity"

您也可以将其添加为第一个下方的新活动代码。