Java 将项目导入 Eclipse 后出现“必须覆盖超类方法”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1678122/
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
'Must Override a Superclass Method' Errors after importing a project into Eclipse
提问by Tim H
Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost allof my overridden methods are not formatted correctly, causing the error:
任何时候我必须将我的项目重新导入到 Eclipse 中(如果我重新安装 Eclipse,或者更改了项目的位置),几乎所有被覆盖的方法都没有正确格式化,从而导致错误:
The method must override a superclass method
该方法必须覆盖超类方法
It may be noteworthy to mention this is with Android projects for whatever reason, the method argument values are not always populated, so I have to manually populate them myself. For instance:
值得一提的是,无论出于何种原因,对于 Android 项目,方法参数值并不总是填充,因此我必须自己手动填充它们。例如:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
//These arguments have their correct names
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
}
});
will be initially populated like this:
最初将像这样填充:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
//This methods arguments were not automatically provided
public void onCreateContextMenu(ContextMenu arg1, View arg2,
ContextMenuInfo arg3) {
}
});
The odd thing is, if I remove my code, and have Eclipse automatically recreate the method, it uses the same argument names I already had, so I don't really know where the problem is, other then it auto-formatting the method for me.
奇怪的是,如果我删除我的代码,并让 Eclipse 自动重新创建该方法,它会使用我已经拥有的相同参数名称,所以我真的不知道问题出在哪里,否则它会自动格式化方法我。
This becomes quite a pain having to manually recreate ALL my overridden methods by hand. If anyone can explain why this happens or how to fix it. I would be very happy.
不得不手动重新创建我所有被覆盖的方法,这变得非常痛苦。如果有人可以解释为什么会发生这种情况或如何解决它。我会很高兴。
Maybe it is due to the way I am formatting the methods, which are inside an argument of another method?
也许这是由于我格式化方法的方式,这些方法在另一个方法的参数中?
采纳答案by alphazero
Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override
, but in Java 1.5 can only be applied to methods overriding a superclass method).
Eclipse 默认为 Java 1.5,并且您有实现接口方法的类(在 Java 1.6 中可以用 注释@Override
,但在 Java 1.5 中只能应用于覆盖超类方法的方法)。
Go to your project/IDE preferences and set the Java compiler level to 1.6 and also make sure you select JRE 1.6 to execute your program from Eclipse.
转到您的项目/IDE 首选项并将 Java 编译器级别设置为 1.6,并确保您选择 JRE 1.6 以从 Eclipse 执行您的程序。
回答by Paul
With Eclipse Galileoyou go to Eclipse -> Preferences menu item, then select Java and Compiler in the dialog.
使用Eclipse Galileo,您可以转到 Eclipse -> Preferences 菜单项,然后在对话框中选择 Java 和 Compiler。
Now it still may show compiler compliance level at 1.6, yet you still see this problem. So now select the link "Configure Project Specific Settings..." and in there you'll see the project is set to 1.5, now change this to 1.6. You'll need to do this for all affected projects.
现在它仍然可能显示编译器合规性级别为 1.6,但您仍然会看到这个问题。所以现在选择链接“配置项目特定设置...”,在那里你会看到项目设置为 1.5,现在将其更改为 1.6。您需要为所有受影响的项目执行此操作。
This byzantine menu / dialog interface is typical of Eclipse's poor UI design.
这种拜占庭式的菜单/对话框界面是 Eclipse 糟糕的 UI 设计的典型代表。
回答by Rollin_s
In case this happens to anyone else who tried both alphazero and Paul's method and still didn't work.
万一发生这种情况的其他任何人都尝试了 alphazero 和 Paul 的方法,但仍然没有奏效。
For me, eclipse somehow 'cached' the compile errors even after doing a Project > Clean...
对我来说,即使在执行 Project > Clean ... 之后,eclipse 也会以某种方式“缓存”编译错误。
I had to uncheck Project > Build Automatically, then do a Project > Clean, and then build again.
我必须取消选中 Project > Build Automatically,然后执行 Project > Clean,然后再次构建。
Also, when in doubt, try restarting Eclipse. This can fix a lot of awkward, unexplainable errors.
此外,如有疑问,请尝试重新启动 Eclipse。这可以解决许多尴尬的、无法解释的错误。
回答by false9striker
To resolve this issue, Go to your Project properties -> Java compiler -> Select compiler compliance level to 1.6-> Apply.
要解决此问题,请转到您的项目属性 -> Java 编译器 -> 选择编译器合规性级别为 1.6-> 应用。
回答by will824
Guys in my case none of the solutions above worked.
就我而言,上述解决方案均无效。
I had to delete the files within the Project workspace:
我不得不删除项目工作区中的文件:
- .project
- .classpath
- 。项目
- .classpath
And the folder:
和文件夹:
- .settings
- .设置
Then I copied the ones from a similar project that was working before. This managed to fix my broken project.
然后我从以前工作的类似项目中复制了那些。这设法修复了我损坏的项目。
Of course do not use this method before trying the previous alternatives!.
当然,在尝试之前的替代方案之前不要使用这种方法!。
回答by ob.yann
This is my second time encounter this problem. first time according the alphazero's recommendation it worked. but in the second time I set to 1.6 it don't work it just like 'CACHE' this error after clean and rebuild.
这是我第二次遇到这个问题。第一次根据 alphazero 的建议它起作用了。但是在我第二次设置为 1.6 时,它不起作用,就像清理和重建后“缓存”这个错误一样。
Try to switch off 'Build Automatically' as Rollin_s said -> error still here!
尝试像 Rollin_s 所说的那样关闭“自动构建”-> 错误仍然存在!
So I removed the problem project (already set to 1.6) from Package Explorer and import it again -> it start a rebuild and no error this time
所以我从包资源管理器中删除了问题项目(已经设置为 1.6)并再次导入 -> 它开始重建,这次没有错误
Hope this help someone
希望这有助于某人
回答by H?c Huy?n Minh
In my case, none the solutions above works. I have to checkout my source code to another folder. From eclipse select File > Switch workSpaces > Other... and then import code to the new workspaces. it works after that.
就我而言,上述解决方案均无效。我必须将我的源代码签出到另一个文件夹。从 Eclipse 中选择 File > Switch workSpaces > Other...,然后将代码导入新的工作区。它在那之后工作。
回答by ulrich
The answer by Paul worked for me partially. I still had one error then. So, in addition to that, I also had to go to Properties-> Project Facets and there set the Java version from 1.5 to 1.6.
保罗的回答部分对我有用。那时我仍然有一个错误。因此,除此之外,我还必须转到 Properties-> Project Facets,然后将 Java 版本从 1.5 设置为 1.6。
Maybe that helps.
也许这有帮助。
回答by Sunil
Fixing must override a super class method error is not difficult, You just need to change Java source version to 1.6 because from Java 1.6 @Override annotation can be used along with interface method. In order to change source version to 1.6 follow below steps :
修复 must override a super class method 错误并不困难,您只需要将 Java 源版本更改为 1.6,因为从 Java 1.6 @Override 注释可以与接口方法一起使用。要将源版本更改为 1.6,请按照以下步骤操作:
- Select Project , Right click , Properties
- Select Java Compiler and check the check box "Enable project specific settings"
- Now make Compiler compliance level to 1.6
- Apply changes
- 选择项目,右键单击,属性
- 选择 Java Compiler 并选中复选框“Enable project specific settings”
- 现在使编译器合规性级别为 1.6
- 应用更改
回答by phil294
If nothing of the above helps, make sure you have a proper "Execution environment" selected, and notan "Alternate JRE".
如果以上都没有帮助,请确保您选择了正确的“执行环境”,而不是“备用 JRE”。
To be found under:
可在以下位置找到:
Project -> Build Path -> Libraries
项目 -> 构建路径 -> 库
Select the JRE System Library
and click Edit...
.
选择JRE System Library
并单击Edit...
。
If "Alternate JRE ..." is selected, change it to a fitting "Execution Environment" like JavaSE-1.8 (jre1.8.0_60)
. No idea why, but this will solve it.
如果选择了“Alternate JRE ...”,请将其更改为合适的“执行环境”,例如JavaSE-1.8 (jre1.8.0_60)
. 不知道为什么,但这会解决它。