Issue with optionsMenu() in Android
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2777098/android-openoptionsmenu-doesnt-work-in-oncreate">Android - openOptionsMenu doesn’t work in onCreate</a>  </p>
</blockquote>



<p>When I call openOptionsMenu() like this:</p>

<pre><code>@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();
        }
    });

}
</code></pre>

<p>...it works perfectly, but if I call it like this:</p>

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

    openOptionsMenu();      
}
</code></pre>

<p>I get this error and the app gets restarted:</p>

<pre><code>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)
</code></pre>

<p>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.</p>
