2

嗨,我是 android 编程新手,我无法弄清楚我的错误。我从昨天开始就一直在尝试这个,当我在我的 activity_thesis.xml 上单击 Play 后放置它时它可以正确执行,但是在 menu.xml 中它在 logcat 上显示了这个错误,并且它显示了一个消息框,上面写着“应用程序 Droid(进程。 com.sample.droid) 已意外停止。请重试。” 这是我的代码

菜单.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);
        }
    });
}   
}

菜单.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
4

1 回答 1

1

从Menu.xml中删除这行代码

 android:onClick="onClick"

您做错了什么是您还创建了 Button 对象并对其设置了 setOnclicklistener ,这样您就创建了两次侦听器。要么添加

public void onClick(View v){

}

在您的活动文件中并使用它

android:onClick="onClick"

或使用您已经在使用的那个。但只使用一种方式来处理点击监听器。

于 2013-10-06T08:14:32.707 回答