Good day! Can you please tell us how to call the context menu by selecting the option menu item?

In the MainActivity method, the MenuItem actionSetings variable has been MenuItem actionSetings whose MenuItem actionSetings has assigned the value of the desired entry in the optional menu in the onCreate () actionSetings = (MenuItem) findViewById(R.id.action_settings); Following in in the method responsible for processing the selection of an item has determined the action when selecting onOptionsItemSelected(MenuItem item)

 case R.id.action_settings: onContextItemSelected(actionSetings); break; 

Then, in the onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) method onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) the creation of the context menu determined the creation of the menu when selecting our item

 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"); } 

After that, in the onContextItemSelected(MenuItem item) method, some actions were determined by those responsible for the action when selecting one or another element of the context menu.

The problem is that when you emulate an application, when you select that element in the optional menu, which defines how the global variable actionSetings occurs, an error occurs.

Please tell me what the problem is. If possible, then some rules for calling the context menu from the optional one I did not take into account. In the documentation I met PopupMenu, which can easily replace the context, but before this functionality I would like to understand the cause of the error.

Here are 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 Found an error in your code when choosing the action responsible for pressing the button of the optional menu. The string onContextItemSelected (actionSetings); - not true. Tell me, please, how can the context menu be called by clicking on an optional element. The registerForContextMenu(); method registerForContextMenu(); does not allow an actionSetings variable as an argument

    1 answer 1

    You need:

    1. In onOptionsItemSelected() when you click on the desired item, call the context menu using openContextMenu( View v )
    2. Everything. You do not need to store links to menu items in the activit.
    • But I have a problem, the same as for Popup, as well as registerForContextMenu (); - they accept variables of type View, but for me this menu item is defined as MenuItem. - abbath0767
    • @ mamba0767, do you need a context menu right on the line of the usual menu? .. If so, then this is clearly a strange approach. These menus are not designed for this. You need to either subMenu in the normal menu or invoke the context menu on any other View activation (i.e. pass any activation markup element to the argument) - Juriy Spb
    • Perhaps I incorrectly set the task for myself. What we have is OptionsMenu with a number of elements, among which several in menu.xml have the app parameter: showAsAction = "ifRoom", that is, displayed to the left of the three buttons, which brings up the menu. I want to call either Popup or ContextMenu when I click on one of the buttons, which is actually a menu item that always displays - abbath0767
    • @ mamba0767, how the menu is displayed does not matter. It is important that ContextMenu and Popup are opened with a binding to the activation element. Therefore, it is not the menu element that is to be transferred to the methods for opening them, but something from the activation. What button or ImageView , something like that. But definitely not MenuItem - YurySPb
    • one
      Thanks, really misunderstood the specification, thanks - abbath0767