You must set a unique identifier for each menu item in onCreateOptionsMenu and onCreateContextMenu.
For instance:
public static final int CONTEXT_MENU_DELETE = Menu.FIRST; public static final int CONTEXT_MENU_EDIT = CONTEXT_MENU_DELETE + 1; @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, CONTEXT_MENU_DELETE, 1, R.string.delete); menu.add(0, CONTEXT_MENU_EDIT, 2, R.string.edit); }
The same goes for onCreateOptionsMenu and onOptionsItemSelected. You must have a unique constant for each menu option.
Added:
You did not view this tutorial ? The idea is the same. You must set different identifiers in menu.xml :
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu>
And then use these identifiers in onOptionsItemSelected :
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } }
Check out these code blocks from the official Android Menu guide and compare with your own code. You can also post your menu.xml , onCreateOptionsMenu and onOptionsItemSelected , so it would be easy to figure out your problem.
source share