我正在测试Java 8的一些新特性,并将示例复制到我的IDE(最初是Eclipse,然后是IntelliJ),如下所示

Eclipse对lambda表达式不提供任何支持,IntelliJ不断报告错误

此语言级别不支持Lambda表达式

我想知道这是否是我的安装、代码或支持的问题。


当前回答

在intellij 14中,以下设置对我有效。 Intellij14设置

其他回答

在intellij 14中,以下设置对我有效。 Intellij14设置

同样的项目(Android Studio 3.3.2, gradle-4.10.1-all.zip, compileSdkVersion 28, buildToolsVersion '28.0.3') 在新的快速Windows机器上运行良好,在旧的Ubuntu 18.04笔记本电脑上用红色下划线标出Java 8的内容(但是项目在Ubuntu上编译没有错误)。

我只改变了两件事,迫使它停止用红色下划线是排除增量true和dexOptions

compileOptions {
//    incremental true
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

//dexOptions {
//    javaMaxHeapSize "4g"
//}

在应用级build.gradle中。

此解决方案适用于Android Studio 3.0或更高版本。

文件>项目结构>模块>应用>属性选项卡

将Source Compatibility和Target Compatibility都更改为1.8

编辑配置文件

您也可以在相应的版本中直接配置它。gradle文件

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

对我来说,所有的解决方案都不起作用。

这应该是你最后的选择: 打开项目的。iml文件,将LANGUAGE_LEVEL更改为JDK_1_8或任何你想要的版本。 如果这没有帮助,你应该在你的项目根目录下grep“JDK”,找到包含版本设置的文件,并将其设置为你喜欢的版本。

您还应该在source选项卡(Modules部分)上更改源代码的Language Level。