Cant override onOptionsItemSelected


Last Updated:

  1. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    [​IMG]

    "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?
     

    Advertisement
  2. aNewGuy

    aNewGuy New Member

    Joined:
    Jul 22, 2010
    Messages:
    3
    Likes Received:
    0
    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.
     
  3. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
  4. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Well I kind of have to worry about it when Eclipse won't let me compile :p
     
  5. aNewGuy

    aNewGuy New Member

    Joined:
    Jul 22, 2010
    Messages:
    3
    Likes Received:
    0
    I mean, just remove "@override". It's basically just a comment.
     
  6. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    That is not true at all.

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

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    So my issue remains. Why does it tell me this method does not exist in the super class?

    This is my Activity definition:
    Code (Text):
    1. public class ActivityInbox extends ListActivity implements OnClickListener, StateModelListener {
     
  8. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  9. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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...
     
  10. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Hallelujah!

    I added android.view.MenuItem as an import and now it works
     
  11. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  12. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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)
     
  13. bizops

    bizops New Member

    Joined:
    Feb 3, 2012
    Messages:
    1
    Likes Received:
    0
    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?
     
  14. jiminaus

    jiminaus Well-Known Member

    Joined:
    Oct 27, 2011
    Messages:
    191
    Likes Received:
    36
    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.
     

Share This Page

Loading...