0

Possible Duplicate:
Android - openOptionsMenu doesn’t work in onCreate

When I call openOptionsMenu() like this:

@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.home_screen);

    layout = (LinearLayout) findViewById(R.id.wrapper1);
    layout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            openOptionsMenu();
        }
    });

}

...it works perfectly, but if I call it like this:

@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.home_screen);

    openOptionsMenu();      
}

I get this error and the app gets restarted:

11-15 11:34:05.218: E/AndroidRuntime(1663): FATAL EXCEPTION: main
11-15 11:34:05.218: E/AndroidRuntime(1663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toparound.logic/com.toparound.logic.HomeScreen}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.os.Looper.loop(Looper.java:137)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at java.lang.reflect.Method.invoke(Method.java:511)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at dalvik.system.NativeStart.main(Native Method)
11-15 11:34:05.218: E/AndroidRuntime(1663): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.view.Window$LocalWindowManager.addView(Window.java:547)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:630)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:508)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.Activity.openOptionsMenu(Activity.java:2800)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at com.toparound.logic.HomeScreen.onCreate(HomeScreen.java:24)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.Activity.performCreate(Activity.java:5008)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-15 11:34:05.218: E/AndroidRuntime(1663):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

Weird, ahhh!!!, cause I expected that this last way the optionsMenu() would have get populated automatically once the Activity was created.In the other hand if I call openOptionsMenu() inside an onClick event handler it works.Please somebody help me. Thanks in Advance.

Community
  • 1
  • 1
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44

1 Answers1

0

Your activity isn't created yet in the 2nd snippet. If the main activity isn't created, it has no window, so you can't open up a subwindow like a menu. You need to call that after onCreate is finished. Best way is to use a handler or runnable and call openOptionsMenu in it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I don't understand why the activity is created in the first snippet and it isn't in the second one. – Daniel Conde Marin Nov 15 '12 at 16:41
  • In the first snippet, you call the actual open in the click listener. So it won't be called until you tap the screen. By the time you do that, the onCreate function has finished and the window is created. – Gabe Sechan Nov 15 '12 at 16:44