我有一项活动:
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,为什么?我哪里错了?
答案 0 :(得分:2)
清理项目(Project - &gt; Clean)可能会有所帮助。