显示菜单但未调用 onCreateOptionsMenu()
Menu showing but onCreateOptionsMenu() not called
菜单在应用程序的工具栏中可见,我可以打开它并看到菜单项,但是没有调用 onCreateOptionsMenu()
函数,我通过设置断点和调试知道这一点, onOptionsItemSelected()
函数也是如此,它根本没有被调用。我看过其他关于相同问题的堆栈溢出帖子,但我似乎没有犯过任何常见错误。有谁知道问题是什么以及如何解决?
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>
编辑:
在我的 onCreate()
中 MainActivity.java 我有这行代码:
toolBar.inflateMenu(R.menu.menu);
如果删除此行,工具栏中的菜单就会消失。不知道这对我的问题是否有帮助或有任何关系,但只是把它放在那里让你知道。
在 oncreate 函数中,输入这一行:
setSupportActionBar(bottomAppBar)
有关详细信息,请参阅此代码:
https://github.com/ranger163/BottomAppBarImplementation
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
并且使用 android.support.v7.widget.Toolbar
会起作用
菜单在应用程序的工具栏中可见,我可以打开它并看到菜单项,但是没有调用 onCreateOptionsMenu()
函数,我通过设置断点和调试知道这一点, onOptionsItemSelected()
函数也是如此,它根本没有被调用。我看过其他关于相同问题的堆栈溢出帖子,但我似乎没有犯过任何常见错误。有谁知道问题是什么以及如何解决?
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>
编辑:
在我的 onCreate()
中 MainActivity.java 我有这行代码:
toolBar.inflateMenu(R.menu.menu);
如果删除此行,工具栏中的菜单就会消失。不知道这对我的问题是否有帮助或有任何关系,但只是把它放在那里让你知道。
在 oncreate 函数中,输入这一行:
setSupportActionBar(bottomAppBar)
有关详细信息,请参阅此代码: https://github.com/ranger163/BottomAppBarImplementation
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
并且使用 android.support.v7.widget.Toolbar
会起作用