Why do I get "must override a superclass method" with @Override?
The following code generates this error message at the public void onClick
line.
Multiple markers at this line
- implements android.view.View.OnClickListener.onClick
- The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
I can't understand why. This code is taken from numerous examples I've seen. What can possibly be wrong?
private Button audioButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
audioButton = (Button) findViewById(R.id.imageButton1);
audioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View button) {
if (button.isSelected()) {
button.setSelected(false);
}
else {
button.setSelected(true);
}
}
});
}
Check the project's properties and verify that Java Compiler -> Compiler compliance level is set to 1.6 (or a later version).
It worked for me... i am using eclipse 2021.... and ..
This is most likely due to a source code level incompatibility between Java 1.5 and 1.6.
In Java 5, the
@Override
annotation requires that the method is actually overriding a method in a superclass.In Java 6 and later, the
@Override
annotation will also be satisfied if the method is implementing an abstract method in a superclass or interface.
So the most likely reason for seeing this in code that you expect to work is that you are compiling Java 6 (or later) code with a Java 5 compiler (or some other compiler with the compiler's source compliance level set to 5).
MAVEN USERS If you're using Maven for build it can override the eclipse settings during build. So if you set Eclipse to 1.7 but don't specifically set the Maven JDK build version(which at the time of this writing defaults to 1.5), then it will reset eclipse target compiler back to 1.5. Set the Maven compiler as follows.
<build>
...
<plugins>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>