Java 无法在 Eclipse 中编译枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3811012/
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-14 05:22:48  来源:igfitidea点击:

Can not compile enums in Eclipse

javaeclipseenums

提问by Artem

While trying to introduce enums in my Android project in Eclipse, I encountered with the following problem: Compiler says:

在 Eclipse 中尝试在我的 Android 项目中引入枚举时,我遇到了以下问题:编译器说:

  1. The type java.lang.Enum cannot be resolved. It is indirectly referenced from required .class files
  2. The type Enum is not generic; it cannot be parameterized with arguments
  1. 无法解析 java.lang.Enum 类型。它是从所需的 .class 文件间接引用的
  2. Enum 类型不是通用的;它不能用参数参数化

I work under Ubuntu 10.04 and use Eclipse v.3.5.2. Tried to use both Java 6 OpenJDK and Java 6 Sun, but without success.

我在 Ubuntu 10.04 下工作并使用 Eclipse v.3.5.2。尝试同时使用 Java 6 OpenJDK 和 Java 6 Sun,但没有成功。

Can anybody help me to sort out with this issue?

有人可以帮我解决这个问题吗?

Here is the code:

这是代码:

public class MyClass {

  public MyClass() {
  }

  ...

  enum MyEnum {
    CONST1, CONST2, CONST3;
  }
}

回答by Aaron Digulla

Open the properties for the project, select the page "Java Compiler" and set the "Compiler Compliance level" to "1.5" or better.

打开项目的属性,选择页面“Java Compiler”并将“Compiler Compliance level”设置为“1.5”或更高。

Project properties page for the Java Compiler

Project properties page for the Java Compiler

回答by Cullan

Close project the project with the error.

关闭项目有错误的项目。

Open a different project and launch in emulator. Close the alternative project.

打开一个不同的项目并在模拟器中启动。关闭替代项目。

Open the original project with the error which should no longer be evidenced.

打开带有不应再证明的错误的原始项目。

Don't know why it works or if actually launching the app is necessary but it clears the error .

不知道它为什么有效,或者是否需要实际启动应用程序,但它会清除错误。

回答by user2310742

  1. Go to properties of project with the build error (right click > Properties)
  2. View the "Libraries" tab in the "Build Path" section
  3. Find the "JRE System Library" in the list (if this is missing then this error message is not an eclipse bug but a mis-configured project)
  4. Remove the "JRE System Library"
  5. Hit "Add Library ...", Select "JRE System Library" and add the appropriate JRE for the project (eg. 'Workspace default JRE')
  6. Hit "Finish" in the library selection and "OK" in the project properties and then wait for the re-build of the project
  1. 转到带有构建错误的项目属性(右键单击 > 属性)
  2. 查看“构建路径”部分中的“库”选项卡
  3. 在列表中找到“JRE 系统库”(如果没有,则此错误消息不是 Eclipse 错误而是配置错误的项目)
  4. 删除“JRE 系统库”
  5. 点击“添加库...”,选择“JRE 系统库”并为项目添加适当的 JRE(例如“工作区默认 JRE”)
  6. 在库选择中点击“完成”,在项目属性中点击“确定”,然后等待项目的重新构建

Hopefully the error will be resolved ...

希望错误会得到解决......