• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cant override onOptionsItemSelected

  • Thread starter Thread starter Deleted User
  • Start date Start date
D

Deleted User

Guest
5voc29.png


"The method onOptionsItemSelected(MenuItem) of type ActivityLogin must override a superclass method"


Why is this happening? It had no problem letting me override onCreateOptionsMenu. Also, if you look closely, you'll notice that it doesn't care about me typing super.onOptionsItemSelected(item);.

What gives?
 
I was getting that when i was trying to override something else. I think it's a problem with eclipse where this "error" appears when you override an interface method. I wouldn't worry about it unless you really care about javadoc.
 
Well I kind of have to worry about it when Eclipse won't let me compile :P
 
So my issue remains. Why does it tell me this method does not exist in the super class?

This is my Activity definition:
Code:
public class ActivityInbox extends ListActivity implements OnClickListener, StateModelListener {
 
That is not true at all.

@Override tells the compiler to override the original method and use yours instead
.

No it doesn't.

Any non-final method of a non-final class can be overridden in a derived class, and the compiler doesn't need any annotations to make that happen.

If you believe that it's not possible to override a method without using @Override, then how come I've been doing it for about 13 years, and only started to use annotations relatively recently? :)

And how come when I've had the same error message about @Override, I've just removed the annotation and everything worked just fine?

There are times when you can't achieve what you want without annotations. (Such as using the web services functionality that comes with Java 6). But this isn't one of those times.

Mark
 
It has something to do with my imports I think?

If I change

import android.view.Menu;
to
import android.view.Menu.*;

the onCreateOptionsMenu gets the same error...
 
Hallelujah!

I added android.view.MenuItem as an import and now it works
 
There's a blog post here explaining why @Override isn't as straightforward as it might first appear. I believe this is someone who actually worked on the javac compiler at Sun.

@Override
 
It would be nice if Eclipse said "we dont know what this parameter type is" before it said "we dont know what this method with this unknown parameter type is" (without actually specifying the parameter is unknown part)
 
I am facing the same issue - "The method onOptionsItemSelected(MenuItem) of type ActivityXXX must override a superclass method"


- My actvity extends Activity class
- I have imported android.view.MenuItem;
- Java compiler - JDK 1.6

Any suggestion to to know the root cause and get it solved?
 
Remove the import you added and your current override of onOptionsItemSelected.

Then have Eclipse add the overridden method to your Activity subclass for you by going into the Source menu, choosing Override/Implement Methods, selecting onOptionsItemSelected(MenuItem) under Activity, and then finally clicking OK.

Eclipse will add a new override to your class with exactly the right imports and the right spelling.
 
Back
Top Bottom