简体   繁体   English

显示菜单但未调用 onCreateOptionsMenu()

[英]Menu showing but onCreateOptionsMenu() not called

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.该菜单在应用程序的工具栏中可见,我可以打开它并查看菜单项,但是没有调用onCreateOptionsMenu()函数,我通过设置断点和调试知道这一点, onOptionsItemSelected()函数,它根本没有被调用。 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主活动.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:编辑:在 MainActivity.java 的onCreate()中,我有这行代码:

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.不知道这是否对我的问题有帮助或有什么关系,但只是放在那里让你知道。

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

In oncreate function, put this line:在 oncreate 函数中,放置这一行:

 setSupportActionBar(bottomAppBar)

See this code for more details: https://github.com/ranger163/BottomAppBarImplementation有关更多详细信息,请参阅此代码: https : //github.com/ranger163/BottomAppBarImplementation

setSupportActionBar(toolBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and using android.support.v7.widget.Toolbar would work并使用android.support.v7.widget.Toolbar会工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM