为什么运行时无法识别“res/values/strings.xml”中定义的字符串
我有一个活动:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
清理你的项目(项目 -> 清理)可能会有所帮助。
Cleaning your project (Project -> Clean) might help.