Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error:
The method must override a superclass method
It may be noteworthy to mention this is with Android projects for whatever reason, the method argument values are not always populated, so I have to manually populate them myself. For instance:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { //These arguments have their correct names public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { } });
will be initially populated like this:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { //This methods arguments were not automatically provided public void onCreateContextMenu(ContextMenu arg1, View arg2, ContextMenuInfo arg3) { } });
The odd thing is, if I remove my code, and have Eclipse automatically recreate the method, it uses the same argument names I already had, so I don't really know where the problem is, other then it auto-formatting the method for me.
This becomes quite a pain having to manually recreate ALL my overridden methods by hand. If anyone can explain why this happens or how to fix it. I would be very happy.
Maybe it is due to the way I am formatting the methods, which are inside an argument of another method?
When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error.
When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the way by which java achieve Run Time Polymorphism.
A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.
Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override
, but in Java 1.5 can only be applied to methods overriding a superclass method).
Go to your project/IDE preferences and set the Java compiler level to 1.6 and also make sure you select JRE 1.6 to execute your program from Eclipse.
With Eclipse Galileo you go to Eclipse -> Preferences menu item, then select Java and Compiler in the dialog.
Now it still may show compiler compliance level at 1.6, yet you still see this problem. So now select the link "Configure Project Specific Settings..." and in there you'll see the project is set to 1.5, now change this to 1.6. You'll need to do this for all affected projects.
This byzantine menu / dialog interface is typical of Eclipse's poor UI design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With