安卓:选项菜单例外的Andr​​oid 3+ /资源$ NotFoundException:资源ID#0x1090044 [英] Android: Options Menu exception in Android 3+ / Resources$NotFoundException: Resource ID #0x1090044

查看:90
本文介绍了安卓:选项菜单例外的Andr​​oid 3+ /资源$ NotFoundException:资源ID#0x1090044的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一些活动,都有一个选项菜单,这是一样的。它工作正常,只有一个活动(一个ListView子类),这点击菜单按钮时崩溃。

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.

这只是发生在4.x中(也许3.X - 无法检查),而不是2.3或之前。经测试,在仿真器和不同的设备。

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.

有趣的事实:当我旋转设备的活动已经呈现和preSS然后选择菜单按钮后,它工作正常。

Interesting fact: When I rotate the device after the activity has rendered and press the menu button then, it works fine.

此外,菜单工作的,而该列表适配器仍然载荷和列表为空。一旦被填满问题列表中出现(仅2.3以上版本所提到的)

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.

选项菜单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);
}

该异常不能在应用code发生,也不会似乎参考了应用程序的一个的ressource(它们的存在,R被删除/多次重修,清项目等)

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)

堆栈跟踪:
(这似乎涉及到一些XML资源,但菜单并不是基于XML)

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)

你知道吗?大大有助于AP preciated,这是一个pre-发射拦截器。

Any idea? Help greatly appreciated, it's the one pre-launch blocker..

推荐答案

终于解决了这个问题。这是相当隐藏在这一行code(这出现在一个完全不同的位置):

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);

最后一个参数应包含的配置,但包含空值。看来,一直工作到Android的3.x的

The last parameter should contain the configuration, but contained null. Seems that worked till Android 3.x

下面是包含故障线路的老code:

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

(将code是在UrlImageViewHelper( https://github.com/koush/UrlImageViewHelper)类,我们在项目中使用)

(The code is in the UrlImageViewHelper (https://github.com/koush/UrlImageViewHelper) class we use in the project)

打开选项菜单

See also Crash when opening Option Menu

这篇关于安卓:选项菜单例外的Andr​​oid 3+ /资源$ NotFoundException:资源ID#0x1090044的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆