onOptionsItemSelected 返回错误的 ID
(新手)
当我单击菜单时,上述方法返回第一个菜单中的 ID,而不是我单击的菜单。如果我检查菜单的“Title Condensed”,它是正确的。
int id = item.getItemId(); //returns id of an incorrect menu
String Title = (String) item.getTitleCondensed(); //this returns the correct title.
欢迎任何想法。
(Newbe)
When I click on a menu the above method returns an ID from the first menu, not the one I clicked. If I check for the Title Condensed of the menu it is correct.
int id = item.getItemId(); //returns id of an incorrect menu
String Title = (String) item.getTitleCondensed(); //this returns the correct title.
Any ideas welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有同样的问题。构建生成的文件未正确更新。
如果我重新排序 xml 中的菜单项,我会得到相同的效果...构建和惊喜。单击菜单会带来超出预期的其他代码。
清理并重试
I had the same problem. Generated files from the build are not properly updated.
I got the same effect if i reordered the menu items in the xml...build and surprise. Clicking on menu brings other codes than expected.
Do a clean and try again
您应该在 onCreateOptionsMenu 和 onCreateContextMenu 中为每个菜单项设置唯一的 ID。
例如:
onCreateOptionsMenu 和 onOptionsItemSelected 也是如此。每个菜单选项都应该有一个唯一的常量。
添加:
您没有查看本教程吗?
这个想法是一样的。您应该在
menu.xml
中设置不同的 id:然后在
onOptionsItemSelected
中使用这些 id:查看官方 Android 菜单教程中的这些代码块,并与您自己的代码进行比较。您还可以发布您的
menu.xml
、onCreateOptionsMenu
和onOptionsItemSelected
这样就很容易找出您的问题。You should have set each menu item a unique ID in onCreateOptionsMenu and onCreateContextMenu.
For example:
The same is for onCreateOptionsMenu and onOptionsItemSelected. You should have a unique constant for every menu option.
Added:
Didn't you check out this tutorial?
The idea is the same. You should set different ids in
menu.xml
:And then use those ids in
onOptionsItemSelected
:Check out these code blocks from official Android Menu tutorial and compare to your own code. You could also publish your
menu.xml
,onCreateOptionsMenu
andonOptionsItemSelected
so it would be easy to figure out your problem.