问题
My app has a few activities, all have an options menu, which is the same. It works fine, only on one activity (a ListView subclass), it crashes when clicking the menu button.
This only happens in 4.x (maybe 3.x - can't check), but not 2.3 or prior. Tested in the emulator and on different devices.
Interesting fact: When I rotate the device after the activity has rendered and press the menu button then, it works fine.
Also, the menu works while the list adapter still loads and the list is empty. Once the list is filled the problem occurs (only above version 2.3 as mentioned)
The menu itself can be a simple one-liner without a resource at all, it still happens.
Options menu code:
/**
* Prepare the options menu
* @param menu The menu
* @return You must return true for the menu to be displayed; if you return false it will not be shown.
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
title = getString(R.string.optionsmenu_search);
MenuItem item1 = menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, title);
item1.setIcon(R.drawable.ic_menu_search);
return super.onPrepareOptionsMenu(menu);
}
* Handle options menu click
* @param item menu item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
// ...
}
return OptionsMenu.itemSelected(this, item) || super.onOptionsItemSelected(item);
}
The exception does not happen in the app code, nor does it seem to reference to a ressource of the app (they exist, R was deleted/rebuilt many times, project cleared etc)
Stack trace: (it seems to reference some xml resource, but the menu's not xml-based)
04-13 23:45:39.081: E/AndroidRuntime(2933): FATAL EXCEPTION: main
04-13 23:45:39.081: E/AndroidRuntime(2933): android.content.res.Resources$NotFoundException: Resource ID #0x1090044
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.content.res.Resources.getValue(Resources.java:1019)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.content.res.Resources.getLayout(Resources.java:858)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.view.menu.BaseMenuPresenter.getMenuView(BaseMenuPresenter.java:70)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.getIconMenuView(PhoneWindow.java:3298)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:1096)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:559)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:817)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1486)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1813)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3300)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3273)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2436)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.os.Looper.loop(Looper.java:137)
04-13 23:45:39.081: E/AndroidRuntime(2933): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-13 23:45:39.081: E/AndroidRuntime(2933): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 23:45:39.081: E/AndroidRuntime(2933): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 23:45:39.081: E/AndroidRuntime(2933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 23:45:39.081: E/AndroidRuntime(2933): at dalvik.system.NativeStart.main(Native Method)
Any idea? Help greatly appreciated, it's the one pre-launch blocker..
回答1:
Finally solved the problem. It's quite hidden in this line of code (which appears in a totally different location):
mResources = new Resources(mgr, mMetrics, null);
The last parameter should contain the configuration, but contained null. Seems that worked till Android 3.x
Here's the old code containing the trouble line:
private static void prepareResources(Context context) {
if (mMetrics != null)
return;
mMetrics = new DisplayMetrics();
Activity act = (Activity)context;
act.getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
AssetManager mgr = context.getAssets();
mResources = new Resources(mgr, mMetrics, null);
}
This solved the issue (note the last line):
private static void prepareResources(Context context) {
if (mMetrics != null)
return;
mMetrics = new DisplayMetrics();
Activity act = (Activity)context;
act.getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
AssetManager mgr = context.getAssets();
mResources = new Resources(mgr, mMetrics, act.getResources().getConfiguration());
}
(The code is in the UrlImageViewHelper (https://github.com/koush/UrlImageViewHelper) class we use in the project)
See also Crash when opening Option Menu
来源:https://stackoverflow.com/questions/10212650/android-options-menu-exception-in-android-3-resourcesnotfoundexception-res