问题描述
我试图在继承 Fragment
类的类中扩充菜单。
这是我的 OnCreateOptionsMenu()
方法 -
I am trying to inflate a menu in a class that inherits the Fragment
class.Here is my OnCreateOptionsMenu()
method -
@Override
public boolean OnCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.forecastfragment, menu) ;
return true;
}
这会引发以下错误:
我试过:
MenuInflater inflater = getActivity().getMenuInflater();
然后Android Studio突出显示 @Override
in红色和状态:
but then Android Studio highlights @Override
in red and states:
我还试图在同一个类中创建一个 getMenuInflater
方法并让它返回 new MenuInflater(this)
I also tried to create a getMenuInflater
method in the same class and have it return new MenuInflater(this)
public MenuInflater getMenuInflater() {
return new MenuInflater(this);
}
但抛出以下错误:
错误:方法不会覆盖或者从超类型实现方法
error: method does not override or implement a method from a supertype
我该怎么办?
推荐答案
您的 onCreateOptionsMenu
的签名看起来不正确。看看文档
The signature of your onCreateOptionsMenu
doesn't look right. Take a look at the docs here
查看此代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);//Make sure you have this line of code.
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
这篇关于android getMenuInflater()在一个片段子类中 - 无法解析方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!