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