简体   繁体   English

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

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

Hi I'm new to android programming and I can't figure out my error. 嗨,我是android编程的新手,我无法弄清楚我的错误。 Iv' been trying this since yesterday it executes correctly when I put it after I click Play on my activity_thesis.xml but in the menu.xml it shows this error on logcat and it shows a message box that says "The application Droid(process.com.sample.droid) has stopped unexpectedly. Please try again." 自昨天以来,我一直在尝试此操作,因为当我在activity_thesis.xml上单击“播放”后将其正确放置时,它可以正确执行,但是在menu.xml中,它在logcat上显示此错误,并且显示一个消息框,显示“应用程序Droid(进程。 com.sample.droid)意外停止。请重试。” here's my code 这是我的代码

Menu.java 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 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 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>

and Logcat 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

Remove this line of code from your Menu.xml Menu.xml中删除此行代码

 android:onClick="onClick"

What you have done wrong is you have created object of Button also and have setOnclicklistener to it , in this way you have created a listener twice. 您做错了的是,您还创建了Button的对象,并对其设置了setOnclicklistener,这样您就两次创建了一个侦听器。 Either add 要么添加

public void onClick(View v){

}

in your activity file and use it using 在您的活动文件中并使用

android:onClick="onClick"

or use the one already you are using. 或使用您已经在使用的那个。 but only use a single way to handle click listener. 但只能使用一种方法来处理点击监听器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:无法在父级或祖先语句中找到方法btnSubmit(View)for android:onClick - java.lang.IllegalStateException: Could not find method btnSubmit(View) in a parent or ancestor Context for android:onClick java.lang.IllegalStateException:在 android:onClick 属性定义的父或祖先上下文中找不到方法 play(View) - java.lang.IllegalStateException: Could not find method play(View) in a parent or ancestor Context for android:onClick attribute defined java.lang.IllegalStateException:找不到方法saveData(view) - java.lang.IllegalStateException: Could not find a method saveData(view) java.lang.IllegalStateException:找不到方法 - java.lang.IllegalStateException: Could not find method java.lang.IllegalStateException:找不到方法? - java.lang.IllegalStateException: Could not find a method? java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class - java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class java.lang.IllegalStateException:在视图类上定义的 android:onClick 属性的父级或祖先上下文中找不到方法 pay(View) - java.lang.IllegalStateException: Could not find method pay(View) in a parent or ancestor Context for android:onClick attribute defined on view class java.lang.IllegalStateException:在活动类com.test.apps.test.MainActivity的onClick中找不到方法ExitApp(View) - java.lang.IllegalStateException: Could not find a method ExitApp(View) in the activity class com.test.apps.test.MainActivity for onClick 错误:java.lang.IllegalStateException:无法执行android:onClick的方法 - Error: java.lang.IllegalStateException: Could not execute method for android:onClick Kotlin-java.lang.IllegalStateException:无法执行android:onClick的方法 - Kotlin - java.lang.IllegalStateException: Could not execute method for android:onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM