Android Eclipse 中带有覆盖注释的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3735661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:35:45  来源:igfitidea点击:

Bug with Override annotations in Eclipse

androideclipseannotationsoverriding

提问by Simon

I have a annoying problem with @Overrideannotations in Eclipse. Often when i import working projects on a new PC, Eclipse marks some of the @Overrideannotations as wrong. If i remove the annotations everything is fine and Eclipse also indicates that the methods are overriding the parents methods but adding the Override annotation causes the error again. I am currently working on an Android project so it might be a problem with Android and not with Eclipse..

@Override在 Eclipse 中遇到了一个烦人的注释问题。通常,当我在新 PC 上导入工作项目时,Eclipse 会将某些@Override注释标记为错误。如果我删除注释,一切都很好,Eclipse 还表明这些方法正在覆盖父方法,但添加 Override 注释会再次导致错误。我目前正在开发一个 Android 项目,所以它可能是 Android 的问题,而不是 Eclipse 的问题。

回答by Mark B

This is most likely because you are switching between Java 1.5 and Java 1.6. In 1.5 you couldn't mark interface implementations with @Override, but you can in 1.6.

这很可能是因为您要在 Java 1.5 和 Java 1.6 之间切换。在 1.5 中,您无法使用 @Override 标记接口实现,但在 1.6 中可以。

A quick Google search turned up this good explanation of the difference in this annotation between the two versions: http://www.techienuggets.com/CommentDetail?tx=38155

一个快速的谷歌搜索结果很好地解释了两个版本之间的注释差异:http: //www.techienuggets.com/CommentDetail?tx= 38155

Semantics of @Override is different in JDK 1.5 and JDK 1.6. In JDK 1.5, the @Override annotation is not allowed for implementations of methods declared in an interface, while they are allowed in JDK 1.6. For more information, see:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5008260http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6399361http://blogs.oracle.com/ahe/?entry=override

It is fact that the description of the Override annotation was not updated in the JDK API docs. This has been reported as a bug:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6501053bugdatabase/view_bug.do?bug_id=6501053

@Override 的语义在 JDK 1.5 和 JDK 1.6 中是不同的。在 JDK 1.5 中,@Override 注释不允许用于在接口中声明的方法的实现,而在 JDK 1.6 中是允许的。有关更多信息,请参阅:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5008260 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6399361 http://blogs.oracle.com/ahe/ ?条目=覆盖

事实上,JDK API 文档中并未更新 Override 注释的描述。这已被报告为错误:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6501053bugdatabase/view_bug.do?bug_id=6501053

回答by neeraj t

just go to

就去

 window -> prefrences -> java -> compiler 

and set it to 1.6 as notation starts from 1.6 so if compilence level will less then 1.6 it will gives error

并将其设置为 1.6,因为符号从 1.6 开始,因此如果编译级别低于 1.6,则会出现错误

回答by kanaparthikiran

There are a few places where Java Compiler settings are configured.

有几个地方可以配置 Java 编译器设置。

One way is to Window->Preferences->Java->Compiler->Compiler Compliance Level -> set 1.6 or above. Another way is right click on the Project->Properties->Java Compiler->JDK Compliance -> Select 1.6 or above.

一种方法是Window->Preferences->Java->Compiler->Compiler Compliance Level -> set 1.6 or above。另一种方法是右键单击Project->Properties->Java Compiler->JDK Compliance -> Select 1.6 or above.

Also You can unselect "Enable Project Specific Settings", this will eliminate future JDK compiler compliance errors. Remove Multiple instances of JDK Versions , unless different projects need them.

您也可以取消选择“启用项目特定设置”,这将消除未来的 JDK 编译器合规性错误。删除多个 JDK 版本实例,除非不同的项目需要它们。

回答by WarrenFaith

Make sure the used Java SDK is up to date and that eclipse use the right SDK version (if you have more than one installed) and treats your Android project with the right SDK version...

确保使用的 Java SDK 是最新的,并且 eclipse 使用正确的 SDK 版本(如果您安装了多个版本)并使用正确的 SDK 版本处理您的 Android 项目......