无法覆盖 ListFragment 中的 onCreateOptionsMenu
我创建了一个支持手机和平板电脑版本的应用程序,因此我使用 android-support-v4.jar 库。
我的活动扩展了 ListFragment,我尝试覆盖 onCreateOptionsMenu(Menu menu, MenuInflater inflater),如以下链接所示: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentMenuSupport.html
我之前调用过 setHasOptionsMenu。
不幸的是,我似乎无法覆盖 onCreateOptionsMenu()。
这是错误消息:
方法onCreateOptionsMenu(Menu menu, MenuInflater inflater) MyFragment 类型必须重写或实现超类型方法。
我是这样做的:
Public class MyFragment extends ListFragment
I created an app that supports both phone and tablet version so i use the android-support-v4.jar library.
My activity extends the ListFragment and I tried to override the onCreateOptionsMenu(Menu menu, MenuInflater inflater), as in the following link: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentMenuSupport.html
I previously called setHasOptionsMenu.
Unfortunately, it seems that I cannot override onCreateOptionsMenu().
This is the error message:
The method onCreateOptionsMenu(Menu menu, MenuInflater inflater) of
type MyFragment must override or implements a supertype method.
And I did that with:
Public class MyFragment extends ListFragment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
确保导入来自兼容性库,而不是来自 SDK 本身。
Make sure the imports are from the compatibility library and not from the SDK itself.
好吧,我刚刚遇到了同样的问题,尽管这里没有解决它。我正在使用 ActionBarSherlock 库,结果
onCreateOptionsMenu
希望Menu
来自android.support.v4.view.Menu
和 < code>MenuInflater 来自android.view.MenuInflater
,而不是android.support.v4.view.MenuInflater
。别问我为什么。我不知道这是否能解决每个人的问题,所以我将分享我的解决方法:右键单击 Elcipse > 中您希望该方法所在的空白区域。来源>> Overide/Implement 方法...
然后只需从这里找到它,Eclipse 就会自动导入正确的东西。
OK, I just had this same problem, although it wasn't fixed by what is here. I'm using the ActionBarSherlock library and it turns out that
onCreateOptionsMenu
wantsMenu
to be fromandroid.support.v4.view.Menu
andMenuInflater
to be fromandroid.view.MenuInflater
, notandroid.support.v4.view.MenuInflater
. Don't ask me why. I don't know if this will fix everyone, so I'll share how I figured it out:Right click the blank space where you'd like the method to be in Elcipse > Source > Overide/Implement methods...
Then just find it from here, and Eclipse will automatically import the correct things.
我在活动中使用 SherlockActionBar 时遇到了类似的问题。这是我解决问题的设置:
I had a similar issue using the SherlockActionBar on my activity. Here was my setup that fixed the problem:
有同样的问题,但这是因为我在 Fragment 中使用了错误的 onCreateOptionsMenu 方法!
boolean onCreateOptionsMenu(Menu menu) 仅适用于 Activity 。
必须将其移动到包含片段的活动类。
Fragment 有自己的: void onCreateOptionsMenu (菜单菜单、MenuInflater 充气器)
创建选项菜单:http://developer.android.com/guide/topics/ui/menus.html
Had the same problem, but it was because I used the wrong onCreateOptionsMenu method in my Fragment!
boolean onCreateOptionsMenu(Menu menu) is only for Activities.
Had to move it to the activity class containing the Fragment.
Fragment have their own: void onCreateOptionsMenu (Menu menu, MenuInflater inflater)
Creating an Options Menu: http://developer.android.com/guide/topics/ui/menus.html
哎哟!!!那是一件好事!
我在 MyFragment 中导入了 android.view.Menu 而不是 android.support.v4.Menu!
我在这件事上浪费了几个小时!希望这篇文章至少可以帮助其他人。
Ouch!!! That was a good one!
I imported android.view.Menu in MyFragment instead of android.support.v4.Menu!
I lost a few hours on this one! Hope this post can at least help someone else.
试试这个,实际上 IDE 混淆了本机菜单导入和 Sherlock 导入..所以如果我们明确指定它那么它就会被解决..
Try this, actually IDE got confused bw native menu import and Sherlock import..so if we specify it clearly then it will be resolved..
刚刚在 Xamarin 上的活动中遇到了同样的问题。
它期望该方法将 Xamarin.ActionbarSherlockBinding.Views.IMenu 作为参数。
如何查明:
-注释您开始实现的 OnCreateOptionsMenu 方法。
- 在某些工作方法中开始输入 OnCreateOptionsMenu 就像你想调用它一样。
- 从建议列表中选择它。
- 将光标放在 OnCreateOptionsMenu 调用上。
- 按 Command+d 转到程序集浏览器。您将看到实现中的界面。
- 然后将鼠标指针放在它所需要的参数类型上,您将进入该类型实现的界面。
-您将看到它所在的命名空间。
Just had the same issue in an activity on Xamarin.
it was expecting the method to take Xamarin.ActionbarSherlockBinding.Views.IMenu as an argument.
How to find out:
-Comment the OnCreateOptionsMenu method You started to implement.
-In some working method start typing OnCreateOptionsMenu like You want to call it.
-Choose it from the suggestions list.
-Place a cursor on OnCreateOptionsMenu call.
-press Command+d to go to assembly browser. You will see the interface from implementation.
-Then by pressing mouse pointer on the parameter type it takes You will get to the interface of this type implementation.
-And You will see namespace it is in.
我遇到了同样的问题,这就是我使用 Fragment 的 onCreateOptionsMenu 所做的事情。
重写 Fragment 的 onCreate 方法,并确保使用参数值为“true”的 setHasOptionsMenu 方法,让系统知道 Fragment 将使用 OptionsMenu。
然后重写 onCreateOptionsMenu 来膨胀你的菜单 xml 文件(在这个例子中我膨胀了fragmentmenu.xml
I had the same problem and this what I did to use onCreateOptionsMenu of Fragment.
Override the onCreate method of the Fragment and make sure that you use setHasOptionsMenu method with parameter value "true" to let the system know Fragment will use OptionsMenu.
Then override onCreateOptionsMenu to inflate your menu xml file (here in this example I inflated fragmentmenu.xml
我用过 com.actionbarsherlock.view.Menu - 也许它已经改变了?
i used i used com.actionbarsherlock.view.Menu - maybe it has changed since?