Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to override onCreateOptionsMenu in 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 
like image 447
Waza_Be Avatar asked Oct 21 '11 09:10

Waza_Be


2 Answers

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

like image 179
Maria Neumayer Avatar answered Sep 23 '22 14:09

Maria Neumayer


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.

like image 23
Weston Avatar answered Sep 23 '22 14:09

Weston