[英]Can't retrieve the menu item in the ActionBar
So i have this menu: 所以我有这个菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Settings">
</item>
I add it to the View like this: 我像这样将其添加到视图中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
And then i would like to set the Text before it really gets shown and it thought i would have to do it like this: 然后我想在文本真正显示之前设置文本,它认为我必须这样做:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.getItem(R.id.action_settings);
item.setTitle(langRes.getTextForKey("settings"));
return super.onPrepareOptionsMenu(menu);
}
I get an IndexOutofBoundsException and the Menu when i debug it in the onPrepareOptionsMenu doesn't contain any Items!! 当我在onPrepareOptionsMenu中调试它时,我得到一个IndexOutofBoundsException和菜单,其中不包含任何项目! What am i doing wrong please?
请问我做错了什么?
You have to use MenuItem item = menu.findItem(R.id.action_settings);
您必须使用
MenuItem item = menu.findItem(R.id.action_settings);
instead of MenuItem item = menu.getItem(R.id.action_settings);
而不是
MenuItem item = menu.getItem(R.id.action_settings);
then your problem will resolved. 那么您的问题就会解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.