为什么运行时无法识别“res/values/strings.xml”中定义的字符串

发布于 2025-01-02 14:37:07 字数 2710 浏览 1 评论 0原文

我有一个活动:

public class Notepadv1 extends ListActivity {

   public static final int INSERT_ID = Menu.FIRST;
   ...
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

        boolean result = super.onCreateOptionsMenu(menu);

        menu.add(0, INSERT_ID, 0, R.string.menu_insert); //problem here

        return result;
    }
  ...
}

我的 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Notepad v1</string>
    <string name="no_notes">No Notes Yet</string>
    <string name="menu_insert">Add Item</string>
</resources>

然后,我在模拟器上运行我的应用程序。应用程序启动后,当我按下“菜单”按钮时,我希望菜单上会显示“添加项目”选项,但出现错误:

E/AndroidRuntime(  631): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  631): android.content.res.Resources$NotFoundException: String resource ID #0x7f040002
E/AndroidRuntime(  631):    at android.content.res.Resources.getText(Resources.java:200)
E/AndroidRuntime(  631):    at android.content.res.Resources.getString(Resources.java:253)
E/AndroidRuntime(  631):    at com.android.internal.view.menu.MenuBuilder.add(MenuBuilder.java:397)
E/AndroidRuntime(  631):    at com.android.demo.notepad1.Notepadv1.onCreateOptionsMenu(Notepadv1.java:55)
E/AndroidRuntime(  631):    at android.app.Activity.onCreatePanelMenu(Activity.java:2123)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:305)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:550)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1192)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1636)
E/AndroidRuntime(  631):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368)
E/AndroidRuntime(  631):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338)
E/AndroidRuntime(  631):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1641)
E/AndroidRuntime(  631):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  631):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  631):    at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(  631):    at java.lang.reflect.Method.invokeNative(Native Method)
...

似乎Android 运行时无法识别我在 res/values/strings.xml 中定义的 "menu_insert" vaue,为什么?我哪里错了?

I have an activity:

public class Notepadv1 extends ListActivity {

   public static final int INSERT_ID = Menu.FIRST;
   ...
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

        boolean result = super.onCreateOptionsMenu(menu);

        menu.add(0, INSERT_ID, 0, R.string.menu_insert); //problem here

        return result;
    }
  ...
}

my res/values/strings.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Notepad v1</string>
    <string name="no_notes">No Notes Yet</string>
    <string name="menu_insert">Add Item</string>
</resources>

Then, I run my application on emulator. After the app launched, When I press the "menu" button I expect to have a "Add Item" option to show up on menu, but I got error:

E/AndroidRuntime(  631): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  631): android.content.res.Resources$NotFoundException: String resource ID #0x7f040002
E/AndroidRuntime(  631):    at android.content.res.Resources.getText(Resources.java:200)
E/AndroidRuntime(  631):    at android.content.res.Resources.getString(Resources.java:253)
E/AndroidRuntime(  631):    at com.android.internal.view.menu.MenuBuilder.add(MenuBuilder.java:397)
E/AndroidRuntime(  631):    at com.android.demo.notepad1.Notepadv1.onCreateOptionsMenu(Notepadv1.java:55)
E/AndroidRuntime(  631):    at android.app.Activity.onCreatePanelMenu(Activity.java:2123)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:305)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:550)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1192)
E/AndroidRuntime(  631):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1636)
E/AndroidRuntime(  631):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368)
E/AndroidRuntime(  631):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338)
E/AndroidRuntime(  631):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1641)
E/AndroidRuntime(  631):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  631):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  631):    at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(  631):    at java.lang.reflect.Method.invokeNative(Native Method)
...

It seems the android runtime can not recogonize my "menu_insert" vaue defined in res/values/strings.xml , why? where am I wrong?

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

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

发布评论

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

评论(1

ぶ宁プ宁ぶ 2025-01-09 14:37:13

清理你的项目(项目 -> 清理)可能会有所帮助。

Cleaning your project (Project -> Clean) might help.

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