三星设备上的菜单键崩溃

时间:2016-03-01 10:48:21

标签: java android

我收到三星设备用户的崩溃报告(Galaxy Note 10.1 2014版(lt03wifi),Galaxy Note 10.1 2014版(lt03lte),Galaxy Note3(hlte),Galaxy Note3 Neo(hl3g)等)运行Android 5.1。 当用户按下我的应用程序中的物理菜单键时,将抛出以下异常。其他设备可以正常打开菜单并正确显示。

android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2452)
at android.content.res.Resources.getDrawable(Resources.java:1947)
at android.content.Context.getDrawable(Context.java:409)
at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:810)
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:1092)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:2179)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2625)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4881)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4836)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4300)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4353)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4319)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4327)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4502)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4300)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4353)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4319)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4327)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4300)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4353)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4319)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4478)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4720)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2811)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2403)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2394)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2788)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

如何解决此问题?

我的活动onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_develop, menu);
    StyleApp.styleMenu(menu,this);
    return true;
}

styleMenu函数:

public static void styleMenu(Menu menu,Context c) {
    ColorFilter colorFilter = getColorFilter(c);

    for(int i=0;i<menu.size();i++){
        MenuItem item=menu.getItem(i);
        Drawable icon=item.getIcon();
        icon.setColorFilter(colorFilter);
    }
}

2 个答案:

答案 0 :(得分:0)

根据我的心态应用程序遇到Resources的问题。

如果您通过按菜单键访问资源,请使用以下代码准备资源。

DisplayMetrics mMetrics = new DisplayMetrics();
Activity act = (Activity)mContext;
mContext.getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
AssetManager mgr = context.getAssets();
Resources mResources = new Resources(mgr, mMetrics, act.getResources().getConfiguration());

现在访问资源

比如说:

String color = mResources.getString(R.string.application_name);

希望这会对你有所帮助。

答案 1 :(得分:0)

我终于通过从

替换我的主题父来修复它
<style name="MyTheme" parent="@android:style/Theme.Material" />

<style name="MyTheme" parent="@android:style/Theme.DeviceDefault" />

在我的theme.xml中。

似乎并非所有三星设备都能正确支持Android默认主题。