Cant override onOptionsItemSelected

Discussion in 'Application Development' started by andywhoa, Jul 21, 2010.

  1. andywhoa

    andywhoa Well-Known Member
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    [​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
    5

    Jul 22, 2010
    3
    0
    5
    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
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
  4. andywhoa

    andywhoa Well-Known Member
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Well I kind of have to worry about it when Eclipse won't let me compile :p
     
  5. aNewGuy

    aNewGuy New Member
    5

    Jul 22, 2010
    3
    0
    5
    I mean, just remove "@override". It's basically just a comment.
     
  6. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    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
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    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
    53

    Sep 14, 2009
    148
    26
    53
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    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
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    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
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Hallelujah!

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

    markb Well-Known Member
    53

    Sep 14, 2009
    148
    26
    53
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    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
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    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
    5

    Feb 3, 2012
    1
    0
    5
    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
    53

    Oct 27, 2011
    191
    36
    53
    Sydney, Australia
    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...