无法覆盖 ListFragment 中的 onCreateOptionsMenu

发布于 2024-12-11 10:18:18 字数 751 浏览 0 评论 0原文

我创建了一个支持手机和平板电脑版本的应用程序,因此我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

蘑菇王子 2024-12-18 10:18:18

确保导入来自兼容性库,而不是来自 SDK 本身。

Make sure the imports are from the compatibility library and not from the SDK itself.

救星 2024-12-18 10:18:18

好吧,我刚刚遇到了同样的问题,尽管这里没有解决它。我正在使用 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 wants Menu to be from android.support.v4.view.Menu and MenuInflater to be from android.view.MenuInflater, not android.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.

舟遥客 2024-12-18 10:18:18

我在活动中使用 SherlockActionBar 时遇到了类似的问题。这是我解决问题的设置:

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

public class LoginActivity extends SherlockActivity{

    ...

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getSupportMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }

    ...

}

I had a similar issue using the SherlockActionBar on my activity. Here was my setup that fixed the problem:

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

public class LoginActivity extends SherlockActivity{

    ...

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getSupportMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }

    ...

}
鸵鸟症 2024-12-18 10:18:18

有同样的问题,但这是因为我在 Fragment 中使用了错误的 onCreateOptionsMenu 方法!

boolean onCreateOptionsMenu(Menu menu) 仅适用于 Activity 。

@Override //For Activities
public boolean onCreateOptionsMenu(Menu menu) { 
...

必须将其移动到包含片段的活动类。

Fragment 有自己的: void onCreateOptionsMenu (菜单菜单、MenuInflater 充气器)

@Override //For Fragments.
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
...

创建选项菜单: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.

@Override //For Activities
public boolean onCreateOptionsMenu(Menu menu) { 
...

Had to move it to the activity class containing the Fragment.

Fragment have their own: void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

@Override //For Fragments.
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
...

Creating an Options Menu: http://developer.android.com/guide/topics/ui/menus.html

定格我的天空 2024-12-18 10:18:18

哎哟!!!那是一件好事!

我在 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.

你的心境我的脸 2024-12-18 10:18:18

试试这个,实际上 IDE 混淆了本机菜单导入和 Sherlock 导入..所以如果我们明确指定它那么它就会被解决..

@Override
    public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
            com.actionbarsherlock.view.MenuInflater inflater) {

}

@Override
    public boolean onOptionsItemSelected(
            com.actionbarsherlock.view.MenuItem item) {
        // TODO Auto-generated method stub

}

Try this, actually IDE got confused bw native menu import and Sherlock import..so if we specify it clearly then it will be resolved..

@Override
    public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
            com.actionbarsherlock.view.MenuInflater inflater) {

}

@Override
    public boolean onOptionsItemSelected(
            com.actionbarsherlock.view.MenuItem item) {
        // TODO Auto-generated method stub

}
巴黎夜雨 2024-12-18 10:18:18
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}
梦幻的味道 2024-12-18 10:18:18

刚刚在 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.

幸福丶如此 2024-12-18 10:18:18

我遇到了同样的问题,这就是我使用 Fragment 的 onCreateOptionsMenu 所做的事情。
重写 Fragment 的 onCreate 方法,并确保使用参数值为“true”的 setHasOptionsMenu 方法,让系统知道 Fragment 将使用 OptionsMenu。

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setHasOptionsMenu(true);
}

然后重写 onCreateOptionsMenu 来膨胀你的菜单 xml 文件(在这个例子中我膨胀了fragmentmenu.xml

@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
     inflater.inflate(R.menu.fragmentmenu, menu);
}

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.

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setHasOptionsMenu(true);
}

Then override onCreateOptionsMenu to inflate your menu xml file (here in this example I inflated fragmentmenu.xml

@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
     inflater.inflate(R.menu.fragmentmenu, menu);
}
亢潮 2024-12-18 10:18:18

我用过 com.actionbarsherlock.view.Menu - 也许它已经改变了?

i used i used com.actionbarsherlock.view.Menu - maybe it has changed since?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文