按钮在Android模拟器中按下

时间:2011-10-25 14:02:01

标签: java android

我最近开始为Android编程,我遇到了一个问题,我无法按下按钮工作并将我发送到另一个屏幕。模拟器崩溃了。我认为这是一个非常简单的问题,但我在网上找不到任何东西,我不明白为什么它会崩溃。按下按钮的代码是:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button directions = (Button)findViewById(R.id.buttonDirections);

        directions.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                Intent i = new Intent(MainMenu.this, Directions.class);
                MainMenu.this.startActivity(i);

            }
        });
    }

10-25 14:54:45.457: DEBUG/dalvikvm(372): Debugger has detached; object registry had 1 entries
10-25 14:54:54.527: DEBUG/dalvikvm(137): GC_EXTERNAL_ALLOC freed 152K, 51% free 2940K/5959K, external 4643K/5293K, paused 88ms
10-25 14:54:57.887: INFO/ActivityManager(69): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.ui.app/.MainMenu } from pid 137
10-25 14:54:58.217: INFO/ActivityManager(69): Start proc com.ui.app for activity com.ui.app/.MainMenu: pid=381 uid=10034 gids={}
10-25 14:54:59.497: INFO/ARMAssembler(69): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x445ed6f0:0x445ed8a8] in 951890 ns
10-25 14:55:00.437: INFO/ActivityManager(69): Displayed com.ui.app/.MainMenu: +2s234ms
10-25 14:55:02.660: INFO/ActivityManager(69): Starting: Intent { cmp=com.ui.app/.Directions } from pid 381
10-25 14:55:02.701: DEBUG/AndroidRuntime(381): Shutting down VM
10-25 14:55:02.701: WARN/dalvikvm(381): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-25 14:55:02.721: ERROR/AndroidRuntime(381): FATAL EXCEPTION: main
10-25 14:55:02.721: ERROR/AndroidRuntime(381): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ui.app/com.ui.app.Directions}; have you declared this activity in your AndroidManifest.xml?
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.app.Activity.startActivityForResult(Activity.java:2827)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.app.Activity.startActivity(Activity.java:2933)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at com.ui.app.MainMenu$1.onClick(MainMenu.java:22)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.view.View.performClick(View.java:2485)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.view.View$PerformClick.run(View.java:9080)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.os.Handler.handleCallback(Handler.java:587)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.os.Looper.loop(Looper.java:123)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at java.lang.reflect.Method.invoke(Method.java:507)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-25 14:55:02.721: ERROR/AndroidRuntime(381):     at dalvik.system.NativeStart.main(Native Method)
10-25 14:55:02.740: WARN/ActivityManager(69):   Force finishing activity com.ui.app/.MainMenu
10-25 14:55:03.268: WARN/ActivityManager(69): Activity pause timeout for HistoryRecord{40656c60 com.ui.app/.MainMenu}
10-25 14:55:05.309: INFO/Process(381): Sending signal. PID: 381 SIG: 9

尝试建议后的Logcat

10-25 15:28:44.931: INFO/ActivityManager(69): Starting: Intent { cmp=com.menus.app/.GetDirections } from pid 338
10-25 15:28:44.931: DEBUG/AndroidRuntime(338): Shutting down VM
10-25 15:28:44.939: WARN/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-25 15:28:44.959: ERROR/AndroidRuntime(338): FATAL EXCEPTION: main
10-25 15:28:44.959: ERROR/AndroidRuntime(338): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.menus.app/com.menus.app.GetDirections}; have you declared this activity in your AndroidManifest.xml?
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.app.Activity.startActivityForResult(Activity.java:2827)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.app.Activity.startActivity(Activity.java:2933)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at com.menus.app.HomeMenu$1.onClick(HomeMenu.java:22)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.view.View.performClick(View.java:2485)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.view.View$PerformClick.run(View.java:9080)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.os.Handler.handleCallback(Handler.java:587)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.os.Looper.loop(Looper.java:123)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at java.lang.reflect.Method.invoke(Method.java:507)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-25 15:28:44.959: ERROR/AndroidRuntime(338):     at dalvik.system.NativeStart.main(Native Method)
10-25 15:28:44.989: WARN/ActivityManager(69):   Force finishing activity com.menus.app/.HomeMenu
10-25 15:28:45.509: WARN/ActivityManager(69): Activity pause timeout for HistoryRecord{40636730 com.menus.app/.HomeMenu}
10-25 15:28:47.379: INFO/Process(338): Sending signal. PID: 338 SIG: 9
10-25 15:28:47.454: INFO/ActivityManager(69): Process com.menus.app (pid 338) has died.
10-25 15:28:47.454: INFO/WindowManager(69): WIN DEATH: Window{4070afd8 com.menus.app/com.menus.app.HomeMenu paused=false}
10-25 15:28:47.659: WARN/InputManagerService(69): Got RemoteException sending setActive(false) notification to pid 338 uid 10034
10-25 15:28:57.269: WARN/ActivityManager(69): Activity destroy timeout for HistoryRecord{40636730 com.menus.app/.HomeMenu}
10-25 15:29:00.150: WARN/ActivityManager(69): finishReceiver called but no pending broadcasts

我还重新制作了这个项目,因为我认为它可能与一个类似命名的项目相冲突,所以名称不同。

3 个答案:

答案 0 :(得分:1)

可能你还没有在清单

中宣布你的路线类

将其添加到AndroidManifest.xml文件

<activity android:name=".Directions" android:label="Directions" />

答案 1 :(得分:0)

堆栈跟踪是什么样的(使用LogCat)? 99.9%的时间用于此类问题,堆栈跟踪将为您提供答案。

猜测,你在MainMenu.this上看到一个nullpointer异常,但堆栈跟踪会告诉你肯定。

[更新堆栈跟踪后编辑]

新的堆栈跟踪表明您现在遇到的问题是Android无法识别您的路线活动。

检查你的AndroidManifest.xml,也是dymmeh建议的。我建议尝试:

<activity android:name="com.ui.app.Directions" android:label="Directions"></activity>

当然,确保Directions类的包名称是正确的。如果您的Activity类位于非标准位置,则.Directions表示法通常会失败。

答案 2 :(得分:0)

您可能忘记在Android Manifest.xml文件中包含Directions.class 因此,Android无法找到活动。