Unable to override onCreateOptionsMenu in ListFrag

2019-01-31 18:36发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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

...


}


回答4:

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



回答5:

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.



回答6:

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

}


回答7:

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}


回答8:

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.



回答9:

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


回答10:

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