28
votes

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

10 Answers

55
votes

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

47
votes

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.

26
votes

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

    ...

}
17
votes

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

7
votes

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.

2
votes

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

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

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.

0
votes

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

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