MenuItem.getItemId 返回 0 而不是 ItemId
我遇到有关菜单和菜单项的问题。每当我点击 MenuItem
时,item.getItemId()
总是返回 0。有人知道为什么吗?
public class MenuAct extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.event_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("event", "ItemSelected=" + item.getItemId());// always 0
return true;
}
}
/res/menu/event_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:name="@+id/evt_createEvent"
android:title="1" />
<item
android:name="@+id/menu_evt_abortCreate"
android:title="2" />
<item
android:name="@+id/menu_evt_saveChanges"
android:title="3" />
<item
android:name="@+id/menu_evt_deleteEvent"
android:title="4" />
</menu>
I'm having problems regarding Menus and MenuItems. Whenever I click on a MenuItem
, item.getItemId()
always returns 0. Does anyone know why?
public class MenuAct extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.event_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("event", "ItemSelected=" + item.getItemId());// always 0
return true;
}
}
/res/menu/event_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:name="@+id/evt_createEvent"
android:title="1" />
<item
android:name="@+id/menu_evt_abortCreate"
android:title="2" />
<item
android:name="@+id/menu_evt_saveChanges"
android:title="3" />
<item
android:name="@+id/menu_evt_deleteEvent"
android:title="4" />
</menu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有为菜单项分配任何 ID,因此 Android 无法识别它们并返回 0。
如果您需要项目 ID,则需要通过
android:id
属性提供:You are not assigning any IDs to your menu items, so Android can not know them and returns 0.
If you need the item id, you need to provide it via
android:id
attribute:前面的答案是正确的,但如果您使用的是 Eclipse,也许这还不够。
请使用“布局”编辑器选项卡编辑您的menu.xml文件
然后您的不工作文件
将变成
这可能是内部原因的结果某处出现错误。因此,您必须使用布局选项卡来执行此操作,否则您将继续遇到 getItemId 返回零的问题。
现在我的方法返回正确的 id
The previous answers is correct but if you are using Eclipse, perhaps is it not sufficient.
Please edit your menu.xml files by using the "Layout" Editor Tab
Then your not working file
will become
It is probably the consequence of internal bug somewhere. So you must use the layout tab to do it otherwise you will continue to have such problem with getItemId returning zero.
Now my method is returning the right id