Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener() must override a superclass method?

With this code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
. . .

Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
    buttonAuthorizeUsers.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent configure = new  Intent(OnDemandAndAutomatic_Activity.this, Configure_Activity.class);  
            OnDemandAndAutomatic_Activity.this.startActivity(configure); 
        }
      });

I'm getting:

The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

It would seem that this problem is sometimes caused by a Project | Properties | Java Compiler being set to 1.5.

Although I'm virtually I'd had this problem before, and changed it to 1.6, somehow it WAS 1.5 again.

HOWEVER, that (changing it to 1.6) did not solve the problem. I'm still getting that same err msg, after cleaning, building, and F11ing...???

like image 590
B. Clay Shannon-B. Crow Raven Avatar asked Jan 26 '12 23:01

B. Clay Shannon-B. Crow Raven


3 Answers

I would recommend that you uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above. Otherwise you would have to specify it every time.

If you need a specific compliance level for a specific project, you need to verify every other project that need compliance level 1.6 or above is set to this.

After everything is correctly setup - clean projects and restart Eclipse. Eclipse can be such a bitch some times - this often solves problems for me.

like image 87
mlunoe Avatar answered Oct 21 '22 03:10

mlunoe


Two things to consider:

1) Take a look at your imports - are you sure that View.OnClickListener is imported, but not lets say DialogInterface.OnClickListener

2) OnClickListener is actually an interface, that you are instantiating anonymously. So after all when writing the onClick method you are actually not overriding a super class method, but instead implementing an interface method. Annotating interface methods with @Override is a good practice, but this has been introduced in JDK 6, which means that by the time Android 1.5 or 1.6 was developed this may not has been yet introduced to the java language and hence making it an invalid syntax.

like image 38
asenovm Avatar answered Oct 21 '22 03:10

asenovm


Right below the "Compiler Compliance Level", there are a few options grayed out if the "Use default compliance settings" checkbox is checked: Namely, "Generated .class files compatibility" and "Source compatibility". Verify that both of those are set to 1.6 - If not, either change the default compliance settings, or uncheck that box and tweak them directly.

like image 2
Alexander Lucas Avatar answered Oct 21 '22 03:10

Alexander Lucas