我正在实施OptionsMenu
,并且从项目的菜单文件夹中的xml文件中读取项目,即res/menu/menu.xml
。覆盖OnCreateOptionsMenu
方法和onOptionsItemSelected
后,我收到了Invocation Target Exception
。以下是方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
ItemSelected方法
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(FullscreenActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(FullscreenActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(FullscreenActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(FullscreenActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(FullscreenActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(FullscreenActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
以下是 menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_bookmark"
android:title="Bookmark" />
<item android:id="@+id/menu_save"
android:title="Save" />
<item android:id="@+id/menu_search"
android:title="Search" />
<item android:id="@+id/menu_share"
android:title="Share" />
<item android:id="@+id/menu_delete"
android:title="Delete" />
<item android:id="@+id/menu_preferences"
android:title="Preferences" />
</menu>
logcat的
06-21 12:23:42.875: I/ActivityManager(89): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.app.wabicountermanager/.FullscreenActivity} from pid 2436
06-21 12:23:43.125: I/ActivityManager(89): Start proc com.app.wabicountermanager for activity com.app.wabicountermanager/.FullscreenActivity: pid=2446 uid=10040 gids={3003}
06-21 12:23:45.815: E/AndroidRuntime(2446): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.wabicountermanager/com.app.wabicountermanager.FullscreenActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
06-21 12:23:45.815: E/AndroidRuntime(2446): at com.app.wabicountermanager.FullscreenActivity.onCreate(FullscreenActivity.java:73)
06-21 12:23:46.205: W/ActivityManager(89): Force finishing activity com.app.wabicountermanager/.FullscreenActivity
06-21 12:23:46.725: W/ActivityManager(89): Activity pause timeout for ActivityRecord{410643e8 com.app.wabicountermanager/.FullscreenActivity}
06-21 12:23:57.166: W/ActivityManager(89): Activity destroy timeout for ActivityRecord{410643e8 com.app.wabicountermanager/.FullscreenActivity}
例外详情
WabiCounterManager [Android Application]
WabiCounterManager [Android Application]
DalvikVM[localhost:8652]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1956
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1981
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 123
ActivityThread$H.handleMessage(Message) line: 1147
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4424
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 784
ZygoteInit.main(String[]) line: 551
NativeStart.main(String[]) line: not available [native method]
Thread [<10> Binder Thread #2] (Running)
Thread [<9> Binder Thread #1] (Running)
Daemon Thread [<8> FinalizerWatchdogDaemon] (Running)
Daemon Thread [<7> FinalizerDaemon] (Running)
我没有得到我所缺少的东西。感谢