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.
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).
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.
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>
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.
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
Now it's 2018! For easy reference, see screenshot below.