Java 片段子类中的android getMenuInflater() - 无法解析方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30847096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android getMenuInflater() in a fragment subclass - cannot resolve method
提问by Flame of udun
I am trying to inflate a menu in a class that inherits the Fragment
class.
Here is my OnCreateOptionsMenu()
method -
我正在尝试在继承Fragment
该类的类中扩充菜单。这是我的OnCreateOptionsMenu()
方法 -
@Override
public boolean OnCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.forecastfragment, menu) ;
return true;
}
This raises the following error:
这会引发以下错误:
Cannot resolve method 'getMenuInflater()'
无法解析方法“getMenuInflater()”
I tried :
我试过 :
MenuInflater inflater = getActivity().getMenuInflater();
but then Android Studio highlights @Override
in red and states:
但随后 Android Studio@Override
以红色突出显示并指出:
Method does not override method from its superclass
方法不会覆盖其超类中的方法
I also tried to create a getMenuInflater
method in the same class and have it return new MenuInflater(this)
我也尝试getMenuInflater
在同一个类中创建一个方法并让它返回new MenuInflater(this)
public MenuInflater getMenuInflater() {
return new MenuInflater(this);
}
but then the following error is thrown :
但随后抛出以下错误:
error: incompatible types: ForecastFragment cannot be converted to Context
error: method does not override or implement a method from a supertype
错误:不兼容的类型:ForecastFragment 无法转换为 Context
错误:方法没有覆盖或实现来自超类型的方法
What do I do?
我该怎么办?
采纳答案by Zain
The signature of your onCreateOptionsMenu
doesn't look right. Take a look at the docs here
你的签名onCreateOptionsMenu
看起来不对。看看这里的文档
Take a look this code
看看这个代码
@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);
}
回答by McGuile
Use this code:
使用此代码:
@Override
public boolean OnCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu) ;
final MenuItem item = menu.findItem(R.id.forecastID);
}
where forecastID is the ID of the item in the menu forcastfragment.xml.
Also add setHasOptionsMenu(true);
in your OnCreateView()
so that the fragment will call the method.
其中forecastID 是菜单forcastfragment.xml 中项目的ID。还要添加setHasOptionsMenu(true);
您,OnCreateView()
以便片段将调用该方法。
As a side, it's standard practice to include the word 'menu' in your menu file names such as 'forecastfragment_menu.xml'. It avoids confusion.
另一方面,标准做法是在菜单文件名中包含“菜单”一词,例如“forecastfragment_menu.xml”。它避免了混乱。
回答by Jordi Castilla
You must use it in this way:
您必须以这种方式使用它:
@Override
public boolean OnCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
return true;
}
回答by Marco Concas
In your fragment class add:
在您的片段类中添加:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.[IDMENU], menu) ;
}
Where [IDMENU] is the XML name of your menu.
其中 [IDMENU] 是菜单的 XML 名称。
Next you need to add inside onCreate or onCreateView method this:
接下来,您需要在 onCreate 或 onCreateView 方法中添加以下内容:
setHasOptionsMenu(true);