Why do I get "must override a superclass method" with @Override?
JavaAndroidCompiler ErrorsJava Problem Overview
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);
}
}
});
}
Java Solutions
Solution 1 - Java
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 ..
Solution 2 - Java
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).
Solution 3 - Java
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>
Solution 4 - Java
If you set the compiler to 1.6 and still get this error, try to check your imports, because what Eclipse does is that it always try to do this
import android.content.DialogInterface.OnClickListener
instead of ->
import android.view.View.OnClickListener
That solves my problem.
Solution 5 - Java
Solution 6 - Java
Putting a View.onCLickListener()
solved the problem to me. My Java Compiler --> Compiler Compliance level is already set to 1.6 but still I was having the same problem.
But changing the code
rdBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onRadioButtonClicked(v);
}
});
to
rdBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onRadioButtonClicked(v);
}
});
solved the problem in my case.
Solution 7 - Java
For me this happened because the Method I wanted to override was package private and I tried to override it from a different package.
Eclipse will additionally put a warning in that case that I didn't notice because of a ton of other warnings
Solution 8 - Java
Now it's 2020! And you should change the compliance to 1.8 !
I tried changing the compliance to 1.6 but it didn't work. And I was trying randomly changing it to other levels like 11, 12 or 13 but it didn't work either.
After reading a bit I found this topic which has helped me to understand: What is “compiler compliance level” in Eclipse?
Reading that the Java compiler can compile code that will run on prior versions of the JVM. I have changed the compiler level to 1.8 and it did solve the problem. So, always aim to the highest compliance level!