74

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);
            }
        }
    });
}
Richard Eng
  • 1,954
  • 6
  • 23
  • 32
  • http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips –  Jan 02 '12 at 05:14
  • If changing compiler to 1.6 don't work then refresh the project and build it. It worked for me. – Vineela Thonupunuri Dec 03 '18 at 21:46

11 Answers11

173

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 ..

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
58

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).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
7

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>
John McDonald
  • 81
  • 1
  • 4
4

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

Now it's 2018! For easy reference, see screenshot below.

enter image description here

Marvin Glenn Lacuna
  • 1,682
  • 1
  • 19
  • 25
1

It's 2020 -

Project>Right Click>Java Compiler>Compiler Compliance Level> Change this to 1.8 [or latest level]

enter image description here

Vikas Piprade
  • 272
  • 2
  • 7
0

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.

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
Amogh Natu
  • 781
  • 1
  • 10
  • 37
0

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

Qw3ry
  • 1,319
  • 15
  • 31
0

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!

0
  1. Change your jdk version to minimum 1.8 in library section of buildpath.
  2. Change your your project facets java version to 1.8 apply and close

Finally your issues will be resolved.

0

for someone using vscode with maven, you can add these settings to your pom.xml to let vscode use the preferred compiler version:

<project>
...
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
...
</project>
Rohim Chou
  • 907
  • 10
  • 16