我完全不知道为什么会这样。选项菜单工作正常,但现在它已不再有效。
当我点击菜单按钮时,菜单打开,我点按它,什么都没有。当我做我的LogCat时它确实注册了MenuItem ......我真的会拉我的头发,我不明白为什么会这样。
06-06 22:19:07.899: DEBUG / MYTAG(23124):愚蠢的咔哒声 id =保存设置项id = 2133000192 ID2 = 2133065728
下面是代码和xml
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
Log.d("MYTAG", "Clicked saved1");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
Log.d("MYTAG", "Stupid clicker id="+item+" item id="+item.getItemId()+" id2="+R.mainmenuSettings.save1);
switch(item.getItemId()){
case R.mainmenuSettings.save1:
Log.d("MYTAG", "Stupid clicker");
break;
}
return super.onOptionsItemSelected(item);
}
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+mainmenuSettings/save1"
android:title="save settings"/>
</menu>
答案 0 :(得分:5)
菜单XML中存在错误。 “id”属性需要以“@ + id /”开头,所以这需要是“@ + id / mainmenuSettingsSave1”或类似的东西。此外,您需要在代码中将其引用为R.id.mainmenuSettingsSave1:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mainmenuSettingsSave1"
android:title="save settings"/>
</menu>
和
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
Log.d("MYTAG", "Clicked saved1");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
Log.d("MYTAG", "Stupid clicker id="+item+" item id="+item.getItemId()+" id2="+R.id.mainmenuSettingsSave1);
switch(item.getItemId()){
case R.id.mainmenuSettingsSave1:
Log.d("MYTAG", "Stupid clicker");
break;
}
return super.onOptionsItemSelected(item);
}