Unable to override onCreateOptionsMenu in ListFrag

2019-01-31 18:57发布

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

10条回答
Animai°情兽
2楼-- · 2019-01-31 19:15
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}
查看更多
贪生不怕死
3楼-- · 2019-01-31 19:16

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-31 19:17

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;
    }

...


}
查看更多
Ridiculous、
5楼-- · 2019-01-31 19:19

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

查看更多
仙女界的扛把子
6楼-- · 2019-01-31 19:27

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.

查看更多
SAY GOODBYE
7楼-- · 2019-01-31 19:28

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.

查看更多
登录 后发表回答