28

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
Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
Waza_Be
  • 39,407
  • 49
  • 186
  • 260

10 Answers10

55

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

Maria Neumayer
  • 3,337
  • 3
  • 27
  • 44
  • I was writing my solution when you replied ;-) You are right, that was the issue. Thanks. – Waza_Be Oct 21 '11 at 09:51
  • 9
    If you come to this question by googling the same error for ActoionBarSherlock, the same solution applyes. Remove the menu import statement and reimport the menu from ABS. The replace getMenuInflater by getSupportMenuInflater. – Snicolas Oct 20 '12 at 07:40
  • 1
    Just to be more clear: you need to override the method that uses sherlock Menu classes, instead of the sdk ones. Also use getSupportMenuInflater() to inflate the lib Menu. – Rafael Sanches Oct 24 '12 at 09:42
47

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.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
Weston
  • 1,882
  • 1
  • 20
  • 26
  • 17
    Note: If you do want to use the inflater you'll want to swap out `getMenuInflater()` with `getSupportMenuInflater()`. – Grimmace Jun 06 '12 at 02:36
  • In my case I forgot to extends SherlockFragment instead Fragment. – Felipe Nov 28 '12 at 21:09
  • You will also need to ensure that your main activity extends ABS like this : "public class MainActivity extends SherlockActivity {" – Britc Dec 01 '13 at 15:16
26

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

    ...

}
gotwo
  • 663
  • 8
  • 16
Chris Smith
  • 688
  • 8
  • 20
17

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

TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
7

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.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
2

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

}
Bala Vishnu
  • 2,588
  • 23
  • 20
1
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}
RZMars
  • 125
  • 8
0

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.

EdgarK
  • 870
  • 7
  • 10
0

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);
}
Debashis
  • 898
  • 8
  • 9
-1

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

siliconeagle
  • 7,379
  • 3
  • 29
  • 41