我已经编写了以下代码,现在在启动程序并单击' Grammer"按钮程序停止。 这是我的主要活动'。
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
logcat将错误指向startActivity(intent);
我无奈地想弄清楚错误。
我发布了logcat错误
02-26 23:33:26.991: D/AndroidRuntime(4604): Shutting down VM
02-26 23:33:26.991: W/dalvikvm(4604): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-26 23:33:27.011: E/AndroidRuntime(4604): FATAL EXCEPTION: main
02-26 23:33:27.011: E/AndroidRuntime(4604): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.grammer/com.example.grammer.Grammer}; have you declared this activity in your AndroidManifest.xml?
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.app.Activity.startActivityForResult(Activity.java:2827)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.app.Activity.startActivity(Activity.java:2933)
02-26 23:33:27.011: E/AndroidRuntime(4604): at com.example.grammer.MainActivity$1.onClick(MainActivity.java:31)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.view.View.performClick(View.java:2485)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.view.View$PerformClick.run(View.java:9080)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.os.Handler.handleCallback(Handler.java:587)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.os.Looper.loop(Looper.java:123)
02-26 23:33:27.011: E/AndroidRuntime(4604): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 23:33:27.011: E/AndroidRuntime(4604): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 23:33:27.011: E/AndroidRuntime(4604): at java.lang.reflect.Method.invoke(Method.java:507)
02-26 23:33:27.011: E/AndroidRuntime(4604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 23:33:27.011: E/AndroidRuntime(4604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 23:33:27.011: E/AndroidRuntime(4604): at dalvik.system.NativeStart.main(Native Method)
这是我的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.grammer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.grammer.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.grammer.grammar.Grammer"
android:label="@string/title_activity_grammer" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.grammer.MainActivity" />
</activity>
<activity
android:name="com.example.grammer.grammar.GrammarRegularActivityActivity"
android:label="@string/title_activity_grammar_regular" >
</activity>
<activity
android:name="com.example.grammer.grammar.Grammer_regular_page2er_regular_page2"
android:label="@string/title_activity_grammer_regular_page2" >
</activity>
<activity
android:name="com.example.grammer.grammar.Exerciseexample.grammer.Exercise"
android:label="@string/title_activity_exercise" >
</activity>
<activity
android:name="com.example.grammer.grammar.Amisare2Amisare2"
android:label="@string/title_activity_amisare2" >
</activity>
</application>
</manifest>
答案 0 :(得分:1)
您的清单中的这一行看起来很可疑,因为它与MainActivity不匹配相同的包(在路径中有一个额外的“.grammar”)
改变这个:
android:name="com.example.grammer.grammar.Grammer"
对此:
android:name="com.example.grammer.Grammer"
或者只是
android:name=".Grammer"
答案 1 :(得分:0)
OnClickListener中的'startActivity(...)'方法调用应该读取MainActivity.this.startActivity(...)。
请记住,监听器中的执行上下文是监听器而不是活动(监听器中的“this”指的是监听器,而不是活动)。