意外停止的原因不明

时间:2013-09-12 06:07:47

标签: android android-intent

以下是我的代码。当我单击“addDebtor”按钮时,它会给我一个错误:应用程序Debt Tracker(进程biz.midl.debttracker)意外停止。请再试一次。

我在IDE中找不到任何错误。我检查了按钮的名称,它匹配。我检查了我的其他活动的名称,也匹配。

有什么想法吗?

public class MainActivity extends Activity {

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

    Button getAnswerButton = (Button) findViewById(R.id.addDebtor);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, addDebt.class));

        }
    });
}  

}

Logcat错误是:

09-12 06:03:49.744: D/AndroidRuntime(246): Shutting down VM
09-12 06:03:49.744: W/dalvikvm(246): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-12 06:03:49.765: E/AndroidRuntime(246): FATAL EXCEPTION: main
09-12 06:03:49.765: E/AndroidRuntime(246): android.content.ActivityNotFoundException: Unable to find explicit activity class {biz.midl.debttracker/biz.midl.debttracker.addDebt}; have you declared this activity in your AndroidManifest.xml?
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.app.Activity.startActivityForResult(Activity.java:2817)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.app.Activity.startActivity(Activity.java:2923)
09-12 06:03:49.765: E/AndroidRuntime(246):  at biz.midl.debttracker.MainActivity$1.onClick(MainActivity.java:22)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.view.View.performClick(View.java:2408)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.view.View$PerformClick.run(View.java:8816)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.os.Handler.handleCallback(Handler.java:587)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.os.Looper.loop(Looper.java:123)
09-12 06:03:49.765: E/AndroidRuntime(246):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-12 06:03:49.765: E/AndroidRuntime(246):  at java.lang.reflect.Method.invokeNative(Native Method)
09-12 06:03:49.765: E/AndroidRuntime(246):  at java.lang.reflect.Method.invoke(Method.java:521)
09-12 06:03:49.765: E/AndroidRuntime(246):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-12 06:03:49.765: E/AndroidRuntime(246):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-12 06:03:49.765: E/AndroidRuntime(246):  at dalvik.system.NativeStart.main(Native Method)
09-12 06:08:54.834: D/AndroidRuntime(277): Shutting down VM
09-12 06:08:54.834: W/dalvikvm(277): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-12 06:08:54.845: E/AndroidRuntime(277): FATAL EXCEPTION: main
09-12 06:08:54.845: E/AndroidRuntime(277): android.content.ActivityNotFoundException: Unable to find explicit activity class {biz.midl.debttracker/biz.midl.debttracker.addDebt}; have you declared this activity in your AndroidManifest.xml?
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.app.Activity.startActivityForResult(Activity.java:2817)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.app.Activity.startActivity(Activity.java:2923)
09-12 06:08:54.845: E/AndroidRuntime(277):  at biz.midl.debttracker.MainActivity$1.onClick(MainActivity.java:22)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.view.View.performClick(View.java:2408)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.view.View$PerformClick.run(View.java:8816)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.os.Handler.handleCallback(Handler.java:587)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.os.Looper.loop(Looper.java:123)
09-12 06:08:54.845: E/AndroidRuntime(277):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-12 06:08:54.845: E/AndroidRuntime(277):  at java.lang.reflect.Method.invokeNative(Native Method)
09-12 06:08:54.845: E/AndroidRuntime(277):  at java.lang.reflect.Method.invoke(Method.java:521)
09-12 06:08:54.845: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-12 06:08:54.845: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-12 06:08:54.845: E/AndroidRuntime(277):  at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

您需要在addDebt中声明AndroidManifest.xml活动。

<activity
            android:name="biz.midl.debttracker.addDebt"
            android:label="@string/app_name" >
  </activity>

此处biz.midl.debttracker是您的packageName

答案 1 :(得分:0)

尝试使用:

 Button getAnswerButton = (Button) findViewById(R.id.addDebtor);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

             Intent start = new Intent (MainActivity.this,
                                        addDebt.class
                                     );
            startActivity(start);

        }
    });

答案 2 :(得分:0)

您要么没有在Manifest文件中添加Activity,要么addDebt.class应该是AddDebt.class,请遵循类名约定。

答案 3 :(得分:0)

将此添加到您的清单

<activity android:name="your package.addDebt" />