当我像这样调用 openOptionsMenu() 时:
@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();
}
});
}
...它工作得很好,但如果我这样称呼它:
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.home_screen);
openOptionsMenu();
}
我收到此错误并重新启动应用程序:
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)
奇怪,啊!!!,因为我希望最后一种方式 optionsMenu() 会在创建 Activity 后自动填充。另一方面,如果我在 onClick 事件处理程序中调用 openOptionsMenu() 它可以工作。请有人帮忙我。提前致谢。