djcmm476
djcmm476

Reputation: 1813

Button presses in Android emulator

I recently began programming for android and I've ran into a problem, I can't get a button press to work and send me to another screen. The emulator just crashes. I think it's a very simple problem but I can't find anything online and I can't see why it crashes. The code for the button press is:

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 after trying suggestions

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

I also remade the project because I thought it might have been conflicting with a similarly named one, so the names are different.

Upvotes: 2

Views: 283

Answers (3)

dymmeh
dymmeh

Reputation: 22306

Likely you haven't declared your Directions class in the manifest

Add this to the file AndroidManifest.xml

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

Upvotes: 1

dcanh121
dcanh121

Reputation: 4695

You might have forgot to include Directions.class in the Android Manifest.xml file Thus, Android is unable to find the activity.

Upvotes: 0

Michael A.
Michael A.

Reputation: 4193

What does the stack trace look like (use LogCat)? 99.9% of the time for this kind of issue, the stack trace will give you the answer.

At a guess, you are seeing a nullpointer exception on MainMenu.this, but the stack trace will tell you for sure.

[Edit after the updated stack trace]

The new stack trace indicates that the problem you are now having is one where Android is unable to identify your Directions Activity.

Check your AndroidManifest.xml, as also suggested by dymmeh. I would suggest trying:

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

Making sure, of course, that the package name for the Directions class is correct. The .Directions notation will often fail if your Activity class is located somewhere nonstandard.

Upvotes: 0

Related Questions