Why should I "override the superclass method" with @Override?

The following code generates this error message in the line public void onClick .

Several markers on this line
- implements android.view.View.OnClickListener.onClick
- The onClick (View) method of type new View.OnClickListener () {} should override the superclass method

I do not understand why. This code is taken from numerous examples that I have seen. What could 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); } } }); } 
+60
java android compiler-errors
Jan 02 2018-12-12T00:
source share
8 answers

Check the project properties and make sure that the Java Compiler -> Compiler Compliance Level is set to 1.6 .

+139
Jan 02 2018-12-12T00:
source share

This is most likely due to source level incompatibility between Java 1.5 and 1.6.

  • In Java 5, the @Override annotation requires this method to actually override the method in the superclass.

  • In Java 6 and later, the @Override annotation @Override also be executed if the method implements the abstract method in a superclass or interface.

So the most likely reason to see this in the code you expect to work is because you are compiling Java 6 (or later) code using the Java 5 compiler (or another compiler with a compiler source matching level set to 5) .

+46
Jan 2 2018-12-12T00:
source share

If you install the compiler in 1.6 and still get this error, try checking your import, because what Eclipse does is that it always tries to do this

 import android.content.DialogInterface.OnClickListener 

instead of β†’

 import android.view.View.OnClickListener 

This solves my problem.

+4
Jul 20 '12 at 4:26
source share

MAVEN USERS If you use Maven to build, it can override eclipse settings during build. Therefore, if you set Eclipse to 1.7, but don’t specifically install the Maven JDK build version (which is 1.5 by default at the time of this writing), then it will reset the eclipse target compiler back to 1.5. Install 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> 
+4
Jul 08 '16 at 2:59
source share

Setting View.onCLickListener() solved the problem for me. My Java Compiler -> Compiler Compliance level is already set to 1.6, but still I had the same problem.

But by changing the code

 rdBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub onRadioButtonClicked(v); } }); 

at

 rdBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub onRadioButtonClicked(v); } }); 

solved the problem in my case.

0
Sep 27 '13 at 18:11
source share

This happened to me because the method that I wanted to override was private , and I tried to override it from another package.

Eclipse will additionally add a warning in this case, which I did not notice due to a ton of other warnings

0
Jan 25 '17 at 11:31 on
source share

Now this is 2018! See the screenshot below for more information.

enter image description here

0
Nov 03 '18 at 7:07
source share

If changing the compiler to 1.6 does not work, update the project and create it.

It worked for me.

0
Dec 03 '18 at 21:46
source share



All Articles