Why is it not possible to tell the compiler to override onClick
I am making an Android application, and I have some buttons.
I want to add some listeners to them, so I have implemented android.view.View.OnClickListener
, and added listeners to the buttons.
However, at the onClick开发者_如何学JAVA
method, I can't add the Override
annotation like this:
@Override
public void onClick(View view) { /* With Override */ }
But this works:
public void onClick(View view) { /* Without Override */ }
And if I try to log something in the latter onClick method, it works as intended.
I am using Eclipse, and it keeps telling me that I must override a superclass method.
Why can't I add the Override
annotation to it if I'm overriding it?
I am using Ubuntu 11.4, if that matters. java -version
returns:
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)
Here is the code:
import android.view.View.OnClickListener;
public class Main extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnFoo = (Button) findViewById(R.id.btnFoo);
btnFoo.setOnClickListener(this);
}
// @Override
public void onClick(View v) {
Log.e("foo", "bar");
}
}
PS. I'm using Sun JDK 1.6
EDIT:
I've found this applies to most of the methods I'm trying to use, e.g public void run()
(from Runnable
).
Because there is nothing to override, you're implementing an interface not subclassing from a super class which already has definition for that method. It simply makes no sense.
you can try to change the java sdk version to 1.6 and will be fix it
精彩评论