Menu showing but onCreateOptionsMenu() not called

random1234 :

The menu is visible in the Toolbar of the application and I can open it and see the menu items, however the onCreateOptionsMenu() function is not being called, I know this by setting a break point and debugging, the same goes for the onOptionsItemSelected() function, it is not called at all. I have looked at other stack overflow posts about the same problems but I don't seem to have done any of the usual errors. Does anyone know what the issue is and how to fix it?

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
        case R.id.menuItem1:
            Toast.makeText(this, "menuItem1 selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menuItem2:
            Toast.makeText(this, "menuItem2 selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menuItem3:
            Toast.makeText(this, "menuItem3 selected", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menuItem1"
        android:title="One"
        app:showAsAction="never"/>

    <item
        android:id="@+id/menuItem2"
        android:title="Two"
        app:showAsAction="never"/>

    <item
        android:id="@+id/menuItem3"
        android:title="Three"
        app:showAsAction="never"/>

</menu>

Edit: In my onCreate() in MainActivity.java I have this line of code:

toolBar.inflateMenu(R.menu.menu);

If I remove this line, the menu in the Toolbar disappears. Don't know if this helps or has anything to do with my issue, but just putting out there so you know.

Mani Vasagam :
 Toolbar toolbar = findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayShowTitleEnabled(false);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25114&siteId=1