0

我无法打开活动。在我的第一个活动 (activity_main) 中,我有 3 个按钮。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:stretchColumns="1">

<TableLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FFFF"
    android:id="@+id/root"
    >

    <TableRow android:layout_margin="0dip"
        android:id="@+id/row1">
        <Button android:id="@+id/button01"
            android:background="#FFFFFF"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:hint="@string/buttonText1"
            android:layout_marginTop="60dip"
            android:layout_marginBottom="15dip"
            android:padding="15dip" />

        </TableRow>

    <TableRow android:layout_margin="0dip"
        android:id="@+id/row2">
        <Button android:id="@+id/button02"
            android:background="#FFFFFF"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:hint="@string/buttonText2"
            android:padding="15dip" />

        </TableRow>

    <TableRow android:layout_margin="0dip"
        android:id="@+id/row3">
        <Button android:id="@+id/button03"
            android:background="#FFFFFF"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:layout_marginTop="15dip"
            android:hint="@string/buttonText3"
            android:onClick="browseExercises"
            android:padding="15dip" />

        </TableRow>

</TableLayout>
</LinearLayout>

我单击第三个按钮,然后我的第二个活动打开,其中包含两个附加按钮。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:stretchColumns="1">

<TableLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FFFF"
    android:id="@+id/root"
    >

    <TableRow android:layout_margin="0dip"
        android:id="@+id/row1">
        <Button android:id="@+id/button01"
            android:background="#FFFFFF"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:hint="@string/alphabetical"
            android:layout_marginTop="60dip"
            android:layout_marginBottom="15dip"
            android:onClick="Alphabetical"
            android:padding="15dip" />

        </TableRow>

    <TableRow android:layout_margin="0dip"
        android:id="@+id/row2">
        <Button android:id="@+id/button99999"
            android:background="#FFFFFF"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:hint="@string/group"
            android:onClick="muscleGroups"
            android:padding="15dip" />

        </TableRow>



</TableLayout>

</LinearLayout>

我单击这些按钮中的任何一个,然后我收到一条错误消息,告诉我“不幸的是,Test_Project 已停止”。

我已经为这两个按钮中的每一个创建了一个活动,并且这两个按钮的所有内容都以与我创建第二个活动时完全相同的方式进行编码,该活动打开得很好,并且具有我可以单击而不会导致任何错误的按钮。我想我在这里遗漏了一些基本的东西,就像我创建了太多的活动一样,他们无法回顾我的 MainActivity.java 文件来寻找应该打开下一个活动的方法。这是我的 MainActivity.java 代码:

package com.example.test_project;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void browseExercises(View view){
        Intent intent = new Intent(this, BrowseExercises.class);
        startActivity(intent);
    }

    public void Alphabetical(View view){
        Intent intent = new Intent(this, Alphabetical.class);
        startActivity(intent);
    }


    public void muscleGroups(View view){
        Intent intent = new Intent(this, MuscleGroups.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test_project"
    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="com.example.test_project.MainActivity"
            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="com.example.test_project.BrowseExercises"
            android:label="@string/title_activity_browse_exercises" 

            >
        </activity>
        <activity
            android:name="com.example.test_project.MuscleGroups"
            android:label="@string/title_activity_muscle_groups" >
        </activity>
        <activity
            android:name="com.example.test_project.Alphabetical"
            android:label="@string/title_activity_alphabetical" >
        </activity>
    </application>

</manifest>

还有我的 logcat 文件:

04-20 13:46:57.632: W/dalvikvm(26747): threadid=1: thread exiting with uncaught exception (group=0x40dc42a0)
04-20 13:46:57.639: E/AndroidRuntime(26747): FATAL EXCEPTION: main
04-20 13:46:57.639: E/AndroidRuntime(26747): java.lang.IllegalStateException: Could not find a method muscleGroups(View) in the activity class com.example.test_project.BrowseExercises for onClick handler on view class android.widget.Button with id 'button99999'
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.view.View$1.onClick(View.java:3587)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.view.View.performClick(View.java:4106)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.view.View$PerformClick.run(View.java:17052)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.os.Handler.handleCallback(Handler.java:615)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.os.Looper.loop(Looper.java:137)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.app.ActivityThread.main(ActivityThread.java:5059)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at java.lang.reflect.Method.invoke(Method.java:511)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at dalvik.system.NativeStart.main(Native Method)
04-20 13:46:57.639: E/AndroidRuntime(26747): Caused by: java.lang.NoSuchMethodException: muscleGroups [class android.view.View]
04-20 13:46:57.639: E/AndroidRuntime(26747):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at java.lang.Class.getMethod(Class.java:915)
04-20 13:46:57.639: E/AndroidRuntime(26747):    at android.view.View$1.onClick(View.java:3580)
04-20 13:46:57.639: E/AndroidRuntime(26747):    ... 11 more
04-20 13:46:59.202: I/Process(26747): Sending signal. PID: 26747 SIG: 9

可能是一些非常简单的事情,我只是还不知道,但任何帮助将不胜感激!!!!

4

2 回答 2

0

您是否为所有活动制作了 Java 文件?就像您要打开其他活动一样,它应该满足以下要求:

  1. 应在清单文件中提及并采取适当的措施
  2. 应该有一个对应于该活动的 java 文件
  3. 应该定义布局文件,或者您应该通过代码创建布局
  4. 应正确声明意图。

如果仍然存在问题,请添加更多您的意图调用代码和 java 文件。

于 2013-04-20T18:18:23.953 回答
0

通过查看您的评论,我猜您还没有为其他活动创建 JAVA 文件。例如,您必须BrowseExercise.java在主要活动所在的目录中创建文件。然后你需要在AndroidManifest.xml文件中定义这个Activity。一个简单的方法是点击+左上角(或类似的东西),然后点击 Android , New Activity 。然后,您只需选择要使用的活动类型。

于 2013-04-20T18:02:38.847 回答