簡體   English   中英

java.lang.IllegalStateException:找不到方法onClick(View)

[英]java.lang.IllegalStateException: Could not find a method onClick(View)

嗨,我是android編程的新手,我無法弄清楚我的錯誤。 自昨天以來,我一直在嘗試此操作,因為當我在activity_thesis.xml上單擊“播放”后將其正確放置時,它可以正確執行,但是在menu.xml中,它在logcat上顯示此錯誤,並且顯示一個消息框,顯示“應用程序Droid(進程。 com.sample.droid)意外停止。請重試。” 這是我的代碼

Menu.java

public class Menu extends Activity 
{

Button beginner, learner;

@Override
protected void onCreate(Bundle MenuButtons) 
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(MenuButtons);
    setContentView(R.layout.menu);

    beginner = (Button) findViewById(R.id.btnBeginner);
    learner = (Button) findViewById(R.id.btnLearner);

    beginner.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            setContentView(R.layout.beginner);
        }
    });
    learner.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            setContentView(R.layout.gameplay);
        }
    });
}   
}

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/categories" >

<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
    android:id="@+id/btnLearner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnBeginner"
    android:layout_centerHorizontal="true"
    android:background="@drawable/learner_menu"
    android:onClick="onClick" />

<Button
    android:id="@+id/btnBeginner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/beginner_menu"
    android:onClick="onClick" />

</RelativeLayout>

</ScrollView>

</LinearLayout>

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thesis.logipic"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ThesisActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.THESISACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.MENU" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Gameplay"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.GAMEPLAY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Beginner"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.BEGINNER" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

</manifest>

Logcat

10-06 15:40:19.995: D/AndroidRuntime(342): Shutting down VM
10-06 15:40:19.995: W/dalvikvm(342): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-06 15:40:20.014: E/AndroidRuntime(342): FATAL EXCEPTION: main
10-06 15:40:20.014: E/AndroidRuntime(342): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.thesis.logipic.ThesisActivity for onClick handler on view class android.widget.Button with id 'btnBeginner'
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.view.View$1.onClick(View.java:2059)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.view.View.performClick(View.java:2408)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.view.View$PerformClick.run(View.java:8816)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.os.Handler.handleCallback(Handler.java:587)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.os.Looper.loop(Looper.java:123)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-06 15:40:20.014: E/AndroidRuntime(342):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:40:20.014: E/AndroidRuntime(342):  at java.lang.reflect.Method.invoke(Method.java:521)
10-06 15:40:20.014: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-06 15:40:20.014: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-06 15:40:20.014: E/AndroidRuntime(342):  at dalvik.system.NativeStart.main(Native Method)
10-06 15:40:20.014: E/AndroidRuntime(342): Caused by: java.lang.NoSuchMethodException: onClick
10-06 15:40:20.014: E/AndroidRuntime(342):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
10-06 15:40:20.014: E/AndroidRuntime(342):  at java.lang.Class.getMethod(Class.java:985)
10-06 15:40:20.014: E/AndroidRuntime(342):  at android.view.View$1.onClick(View.java:2052)
10-06 15:40:20.014: E/AndroidRuntime(342):  ... 11 more

Menu.xml中刪除此行代碼

 android:onClick="onClick"

您做錯了的是,您還創建了Button的對象,並對其設置了setOnclicklistener,這樣您就兩次創建了一個偵聽器。 要么添加

public void onClick(View v){

}

在您的活動文件中並使用

android:onClick="onClick"

或使用您已經在使用的那個。 但只能使用一種方法來處理點擊監聽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM