1

I have a problem with this on Eclipse

        OnItemClickListener onClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), adapter.getItem(position), Toast.LENGTH_LONG);
            Log.i("ITEM CLICK", adapter.getItem(position));
        }
        };

Eclipse is telling me that onItemClick must override a superclass method... and i Have to remove @Override annotation. But it's overriding a method... or isn't it?

Thanks

Javier Manzano
  • 4,761
  • 16
  • 56
  • 86
  • have you imported correctly, something like import android.widget.AdapterView.OnItemClickListener; is present? – nandeesh Jan 08 '12 at 16:13
  • 1
    i would be curious to know who down voted to answers. I felt the answers were correct. If it works @mccrank please accept it. I'm upvoting for what i feel is correct – san Jan 08 '12 at 17:19

4 Answers4

2

In Java 1.5 @Override on the implementation of an interface method is considered incorrect. In Java 1.6 otoh, @Override on interface implementations is completely valid.

If you switch your compiler compliance level to 1.6 in Eclipse you'll be allowed to sprinkle your interface implementations with all the @Overrides you want.

Project properties -> Java Compiler & alter the "Compiler compliance level" to 1.6.

Jens
  • 16,853
  • 4
  • 55
  • 52
  • +1: cause I answered hundreds of comments in my blog with the same explanation (and as there is nothing detailed mention in the question, I would assume the same...) – WarrenFaith Jan 08 '12 at 17:51
1

I am assuming you are using java 1.5 as compilance level that is why you are seeing the error. If you want to avoid these warnings you may change compilance level to 1.6. Here is SO discussion on this topic. Eclipse override error

If you want to use 1.5 compilance level only, you can safely remove override.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Who ever -ve commented, it is better to comment what is wrong with this answer (Which will allow me to learn something too). If something misleading (or) not related to this question, I am happy to edit/remove my answer. – kosa Jan 08 '12 at 17:01
  • +1: cause I answered hundreds of comments in my blog with the same explanation (and as there is nothing detailed mention in the question, I would assume the same...) – WarrenFaith Jan 08 '12 at 17:50
1

May be you imported the wrong OnItemClickListener. If the methods's signature differs the compiler recognizes that it cannot possibly be an override.

Stefan
  • 4,645
  • 1
  • 19
  • 35
0

Using the @Override annotation on methods that implement those declared by an interface is only valid from Java 6 onward. It's an error in Java 5.

Make sure that your IDE projects are setup to use a Java 6 JRE, and that the "source compatibility" is set to 1.6 or greater. Open the Window > Preferences dialog, and browse to Java > Compiler. There you can set the "Compiler compliance level" to 1.6.

Remember that Eclipse can override these global settings for a specific project, so check those too.

sanjeeb
  • 87
  • 1
  • 12