“必须重写超类方法”导入项目到Eclipse后出现错误
每当我必须将我的项目重新导入 Eclipse 时(如果我重新安装了 Eclipse,或更改了项目的位置),几乎所有我的重写方法的格式都不正确,从而导致错误:
该方法必须重写超类方法
值得注意的是,无论出于何种原因,Android 项目中方法参数值并不总是填充,因此我必须自己手动填充它们。例如:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
//These arguments have their correct names
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
}
});
最初将像这样填充:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
//This methods arguments were not automatically provided
public void onCreateContextMenu(ContextMenu arg1, View arg2,
ContextMenuInfo arg3) {
}
});
奇怪的是,如果我删除我的代码,并让 Eclipse 自动重新创建该方法,它会使用我已有的相同参数名称,所以我真的不知道问题出在哪里,除此之外它会为我自动格式化该方法。
这变得非常痛苦,必须手动重新创建我所有覆盖的方法。如果有人可以解释为什么会发生这种情况或如何解决它。我会很高兴。
也许是由于我格式化方法的方式所致,这些方法位于另一个方法的参数内?
Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error:
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:
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
Eclipse 默认使用 Java 1.5,并且您有实现接口方法的类(在 Java 1.6 中可以使用
@Override
进行注释,但在 Java 1.5 中只能应用于覆盖超类方法的方法)。转到您的项目/IDE 首选项并将 Java 编译器级别设置为 1.6,并确保选择 JRE 1.6 从 Eclipse 执行程序。
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).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.
使用 Eclipse Galileo,您可以进入 Eclipse ->; Preferences 菜单项,然后在对话框中选择 Java 和 Compiler。
现在它仍然可能显示编译器合规性级别为 1.6,但您仍然会看到这个问题。现在选择链接“配置项目特定设置...”,您将在其中看到项目设置为 1.5,现在将其更改为 1.6。您需要对所有受影响的项目执行此操作。
这种错综复杂的菜单/对话框界面是 Eclipse 糟糕的 UI 设计的典型表现。
With Eclipse Galileo you go to Eclipse -> Preferences menu item, then select Java and Compiler in the dialog.
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.
This byzantine menu / dialog interface is typical of Eclipse's poor UI design.
万一这种情况发生在尝试过 alphazero 和 Paul 的方法但仍然不起作用的其他人身上。
对我来说,即使在执行了 Project > 之后,Eclipse 也会以某种方式“缓存”编译错误。干净...
我不得不取消选中“项目”>自动构建,然后做一个项目>清理干净,然后重新构建。
此外,如果有疑问,请尝试重新启动 Eclipse。这可以修复许多尴尬的、无法解释的错误。
In case this happens to anyone else who tried both alphazero and Paul's method and still didn't work.
For me, eclipse somehow 'cached' the compile errors even after doing a Project > Clean...
I had to uncheck Project > Build Automatically, then do a Project > Clean, and then build again.
Also, when in doubt, try restarting Eclipse. This can fix a lot of awkward, unexplainable errors.
要解决此问题,请转到您的项目属性 -> Java编译器->选择编译器合规级别为 1.6->申请。
To resolve this issue, Go to your Project properties -> Java compiler -> Select compiler compliance level to 1.6-> Apply.
保罗的回答部分对我有用。那时我仍然有一个错误。因此,除此之外,我还必须转到“属性”->“属性”。 Project Facets 将 Java 版本设置为从 1.5 到 1.6。
也许这有帮助。
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.
Maybe that helps.
如果上述方法均无济于事,请确保您选择了正确的“执行环境”,而不是“备用 JRE”。
可以在以下位置找到:
选择
JRE 系统库
并单击编辑...
。如果选择“备用 JRE ...”,请将其更改为合适的“执行环境”,例如 JavaSE-1.8 (jre1.8.0_60)。不知道为什么,但这会解决它。
If nothing of the above helps, make sure you have a proper "Execution environment" selected, and not an "Alternate JRE".
To be found under:
Select the
JRE System Library
and clickEdit...
.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.就我而言,上述解决方案均无效。
我必须删除项目工作区中的文件:
和文件夹:
然后我从之前正在运行的类似项目中复制了这些文件。这成功修复了我损坏的项目。
当然,在尝试之前的替代方法之前不要使用此方法!
Guys in my case none of the solutions above worked.
I had to delete the files within the Project workspace:
And the folder:
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!.
这是我第二次遇到这个问题。
第一次根据 alphazero 的建议它起作用了。
但第二次我设置为 1.6 时,它不起作用,就像清理和重建后的“缓存”此错误一样。
尝试关闭“自动构建”,正如 Rollin_s 所说 ->错误仍然存在!
所以我从 Package Explorer 中删除了有问题的项目(已设置为 1.6)并再次导入 ->它开始重建,这次没有错误
希望这对某人有帮助
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.
Try to switch off 'Build Automatically' as Rollin_s said -> error still here!
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
Hope this help someone
就我而言,当我将 Maven 项目导入 Eclipse 时,就发生了这个问题。为了解决这个问题,我在
pom.xml
中添加了以下内容:然后在项目的上下文菜单中,转到“Maven -> Update Project ...”,然后按“确定”。
就是这样。希望这有帮助。
In my case this problem happened when I imported a Maven project into Eclipse. To solve this, I added the following in
pom.xml
:Then in the context menu of the project, go to "Maven -> Update Project ...", and press OK.
That's it. Hope this helps.
修复必须重写超类方法错误并不困难,只需将 Java 源版本更改为 1.6,因为从 Java 1.6 开始,@Override 注解可以与接口方法一起使用。要将源版本更改为 1.6,请按照以下步骤操作:
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 :
就我而言,上述解决方案都不起作用。我必须将源代码签出到另一个文件夹。从 eclipse 中选择文件 >切换工作空间 >其他...然后将代码导入到新工作区。之后就可以了。
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.
当您的 Maven 项目使用不同的编译器合规性级别并且 Eclipse IDE 使用不同的编译器合规性级别时,就会发生这种情况。为了解决这个问题,我们需要将 Maven 项目的编译器合规性级别更改为 IDE 使用的级别。
1) 查看 Eclipse IDE 中使用的 Java 编译器合规性级别
*) 窗口 ->首选项->编译器->编译器合规级别:1.8(或 1.7、1.6 等)
2) 要更改 Maven 项目的 Java 编译器合规级别
*) 转到“项目”-> “属性”->选择“Java编译器”->更改编译器合规性级别:1.8(或 1.7、1.6 等)
This happens when your maven project uses different Compiler Compliance level and Eclipse IDE uses different Compiler Compliance level. In order to fix this we need to change the Compiler Compliance level of Maven project to the level IDE uses.
1) To See Java Compiler Compliance level uses in Eclipse IDE
*) Window -> Preferences -> Compiler -> Compiler Compliance level : 1.8 (or 1.7, 1.6 ,, ect)
2) To Change Java Compiler Compliance level of Maven project
*) Go to "Project" -> "Properties" -> Select "Java Compiler" -> Change the Compiler Compliance level : 1.8 (or 1.7, 1.6 ,, ect)
现在是 2020 年 -
项目>右键单击>Java 编译器>编译器合规性级别>将其更改为 1.8 [或最新级别]
It's 2020 -
Project>Right Click>Java Compiler>Compiler Compliance Level> Change this to 1.8 [or latest level]