How do you call a context menu with an element of an option menu?
-
Good day! Please indicate how the contextual menu can be triggered by selecting an element of the optical menu?
In MainActivity method, defined variable
MenuItem actionSetings
of the optical menu in the method onCreate()actionSetings = (MenuItem) findViewById(R.id.action_settings);
After in the method responsible for processing the element ' s choice, it was determined by the choiceonOptionsItemSelected(MenuItem item)
case R.id.action_settings: onContextItemSelected(actionSetings); break;
Then in the method
onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
The person responsible for the creation of the context menu identified the creation of a menu when choosing our element.switch (v.getId()) { case R.id.action_settings: menu.add(0, COLOUR_1_GRAY, 0, "set gray"); menu.add(0, COLOUR_2_GREEN, 1, "set green"); menu.add(0, COLOUR_3_YELLOW, 2, "set yellow"); menu.add(0, COLOUR_4_BLUE, 3, "set blue"); }
Then in the method
onContextItemSelected(MenuItem item)
Some actions have been identified by the person responsible for selecting an element of the context menu.The problem is, when the application is emulated in selecting the element in the optical menu that has been defined as a global variable
actionSetings
There's a mistake.Please tell me what the problem is. If there is a possibility, some rules to challenge a contextual menu from an option that I didn't take into account. The documentation had met PopupMenu, which could easily replace contextual, but in the meantime, the function would like to understand the reason for the error.
Here's the logs.
12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: FATAL EXCEPTION: main 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: Process: com.bestcompany.simplemenu4final, PID: 23971 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke interface method 'int android.view.MenuItem.getItemId()' on a null object reference 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at com.bestcompany.simplemenu4final.MainActivity.onContextItemSelected(MainActivity.java:117) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at com.bestcompany.simplemenu4final.MainActivity.onOptionsItemSelected(MainActivity.java:71) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.app.Activity.onMenuItemSelected(Activity.java:2907) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:621) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:619) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.view.View.performClick(View.java:4785) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19858) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.os.Looper.loop(Looper.java:155) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5696) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 12-14 14:20:31.953 23971-23971/com.bestcompany.simplemenu4final E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
UPD has found a mistake in its code when selecting the action of the contact button of the optical menu. Line onContextItemSelected(actionSetings); - wrong. Please indicate how the contextual menu can be triggered by the application element. Method
registerForContextMenu();
prevents the use of variable actionSetings as an argument
-
You need to:
- ♪
onOptionsItemSelected()
when compressing the necessary element to cause the context menu by the methodopenContextMenu( View v )
- That's it. You don't need to keep references to the menu elements in the activate.
- ♪