打开选项菜单时崩溃

发布于 2024-12-22 06:05:50 字数 2631 浏览 0 评论 0原文

我使用选项菜单进行普通活动,这是我通过以下代码创建的:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, 1, 0, "A");
  menu.add(0, 2, 0, "B");
  menu.add(0, 3, 0, "C");
  return true;
}

在 Android 2.3 及更早版本(2.2、2.1)上一切正常。但是,当我在 Android 4.0(ICS - 模拟器、Galaxy Nexus、Nexus S)上运行此应用程序时,当我按下菜单按钮时,应用程序崩溃并出现以下异常:

W/ResourceType(9263): Failure getting entry for 0x01090044 (t=8 e=68) in package 0 (error -75)
D/AndroidRuntime(9263): Shutting down VM
W/dalvikvm(9263): threadid=1: thread exiting with uncaught exception (group=0x40a4a1f8)
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x1090044
E/AndroidRuntime(9263):
at android.content.res.Resources.getValue(Resources.java:1019)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getLayout(Resources.java:858)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.internal.view.menu.BaseMenuPresenter.getMenuView(BaseMenuPresenter.java:70)
at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.getIconMenuView(PhoneWindow.java:3298)
at com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:1096)
at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:559)
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:817)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1486)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1813)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3300)
at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3273)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2436)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

我无法本地化 ID 为 #0x1090044 的资源(它不在我的 R.java 中)或任何其他文件),但我不知道它应该是什么。也许一些内部菜单布局?我也已经尝试清理该项目。 我知道 Android 4.0 ICS 有新的“设计规则”,并使用操作栏代替旧菜单,但由于许多不同的原因,我需要此代码即使在 ICS 上也能工作。我应该如何编辑此代码才能在 ICS 上工作? 我也已经尝试使用此代码从 XML 文件创建菜单,但有相同的异常。

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);

谢谢您的任何建议!

I have ordinary activity with Option Menu, which I created by this code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, 1, 0, "A");
  menu.add(0, 2, 0, "B");
  menu.add(0, 3, 0, "C");
  return true;
}

On Android 2.3 and older (2.2, 2.1) is everything fine. But when I run this app on Android 4.0 (ICS - emulator, Galaxy Nexus, Nexus S) when I press menu button the app crashdown with this exception:

W/ResourceType(9263): Failure getting entry for 0x01090044 (t=8 e=68) in package 0 (error -75)
D/AndroidRuntime(9263): Shutting down VM
W/dalvikvm(9263): threadid=1: thread exiting with uncaught exception (group=0x40a4a1f8)
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x1090044
E/AndroidRuntime(9263):
at android.content.res.Resources.getValue(Resources.java:1019)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getLayout(Resources.java:858)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.internal.view.menu.BaseMenuPresenter.getMenuView(BaseMenuPresenter.java:70)
at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.getIconMenuView(PhoneWindow.java:3298)
at com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:1096)
at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:559)
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:817)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1486)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1813)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3300)
at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3273)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2436)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

I can't localize resource with ID #0x1090044 (it's not in my R.java or any other files) and I don't know what it should be. Maybe some internal menu layout? I already also tried to clean the project.
I know that there are new "desing rules" for Android 4.0 ICS and using Action Bar instead older menu, but I need this code get working even on ICS for many different reasons. Should I some how edit this code to work even on ICS?
I also already tried creating menu from XML file using this code with same exception.

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);

Thank you for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2024-12-29 06:05:50

最后!问题出在稍微不同的地方。我正在使用这段代码:

Resources standardResources = getResources();
AssetManager assets = standardResources.getAssets();
DisplayMetrics metrics = standardResources.getDisplayMetrics();
Configuration config = new Configuration(); 
// CORRECT is: Configuration config = standardResources.getConfiguration();
config.locale = new Locale(lang);
Resources defaultResources = new Resources(assets, metrics, config);

我之前没有检查这行错误的代码,因为在 2.3 及以下版本上一切都很好。但现在在 4.0 中,内部资源发生了一些变化。我不知道这是否是某种错误(我在活动中多次调用此代码,但仅当我使用选项菜单时才会引发错误),但可以肯定的是我的代码中存在错误。

Finally! The problem was in something slightly different. I was using this code:

Resources standardResources = getResources();
AssetManager assets = standardResources.getAssets();
DisplayMetrics metrics = standardResources.getDisplayMetrics();
Configuration config = new Configuration(); 
// CORRECT is: Configuration config = standardResources.getConfiguration();
config.locale = new Locale(lang);
Resources defaultResources = new Resources(assets, metrics, config);

I didn't check this wrong line of code before because on 2.3 and less everything was fine. But now on 4.0 something was changed in internal resources. I don't know if it is some sort of bug (I'm calling this code for many times in my Activity but only when I use Options menu error is raised), but for sure there was the bug in my code.

放手` 2024-12-29 06:05:50

尝试清理项目并再次运行。如果错误没有消失,请检查 R.java 中菜单 (R.menu.menu) 的 id 值是什么或者它是否存在。

Try clean project and run again. If error isn't gone, check what's the id value in R.java for your menu (R.menu.menu) or if it exists.

所谓喜欢 2024-12-29 06:05:50

根据您的回答,手动更改区域设置的代码会导致错误,但查看堆栈跟踪似乎您可能缺少资源,但无论如何我有类似的区域设置更改代码,并且仅在 ICS 下遇到错误,因此我添加了以下内容希望它可能有所帮助:

要防止出现错误,请将以下内容添加到 AndroidManifest.xml
android:configChanges="locale"

以下是 Activity Javadoc 的摘录,解释了可能发生错误的原因:

在某些特殊情况下,您可能希望绕过重新启动
基于一种或多种类型的配置更改的活动。这是
使用清单中的 android:configChanges 属性完成。为了
您所说的在那里处理的任何类型的配置更改,
您将收到有关您当前活动的电话
onConfigurationChanged 方法而不是重新启动。但是,如果配置更改涉及您不处理的任何配置更改,
Activity 仍将重新启动并 onConfigurationChanged
不会被调用。

According to your Answer the code to manually change locale causes the error but looking at your stack trace it seems that you may have a resource missing, but anyway I have similar Locale change code and was experiencing errors only under ICS so I have added the following in the hope that it may be helpful:

To prevent the error add the following to AndroidManifest.xml
android:configChanges="locale"

Here is an extract from Activity Javadoc that explains why the error may occur:

In some special cases, you may want to bypass restarting of your
activity based on one or more types of configuration changes. This is
done with the android:configChanges attribute in its manifest. For
any types of configuration changes you say that you handle there,
you will receive a call to your current activity's
onConfigurationChanged method instead of being restarted. If a configuration change involves any that you do not handle, however, the
activity will still be restarted and onConfigurationChanged
will not be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文