[英]Android - Menu Item cannot be resolved to a type in onOptionsItemSelected()
I created a simple activity (with a menu) and tried adding menu items but they don't show up when trying to actually give them some function in the onOptionsItemSelected() method. 我创建了一个简单的活动(带有一个菜单)并尝试添加菜单项,但是当他们试图在onOptionsItemSelected()方法中实际给它们一些功能时它们没有出现。 I'm not sure why it's not working, as I did the exact same thing in the menu for the main activity and it worked just fine.
我不确定为什么它不起作用,因为我在主要活动的菜单中做了完全相同的事情并且它工作得很好。 When typing in android.R.id.add_screen_submit_button for example, it is not recognized as existing.
例如,当键入android.R.id.add_screen_submit_button时,它不会被识别为现有。 And if I forcefully just type it in and leave it the message "add_screen_submit_button cannot be resolved or is not a field" comes up.
如果我强行输入并留下“add_screen_submit_button无法解析或不是字段”的消息出现。 The menu is also in the correct folder (I actually just left it as is when creating the Activity).
菜单也在正确的文件夹中(我实际上只是在创建活动时保持原样)。 Thanks in advance.
提前致谢。
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_screen_submit_button"
android:orderInCategory="4"
android:showAsAction="always"
android:title="@string/add_screen_ok"
android:icon="@drawable/accept_icon" />
<item
android:id="@+id/add_screen_cancel_button"
android:orderInCategory="5"
android:showAsAction="always"
android:title="@string/add_screen_cancel"
android:icon="@drawable/cancel_icon" />
</menu>
Here's the code 这是代码
public class AddActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
//setupActionBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case android.R.id.add_screen_submit_button:
Toast.makeText(this, "Map Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Change 更改
android.R.id.home:
to 至
R.id.home:
and the same with the other one. 和另一个一样。
android.R
is for sdk resources and R.id.some_id
is for id
s you create android.R
用于sdk资源, R.id.some_id
用于你创建的id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.