Java android studio:-source 1.7 不支持 lambda 表达式(使用 -source 8 或更高版本来启用 lambda 表达式)

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

android studio : lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)

javaandroidlambda

提问by Chintan Soni

Happy to know that java introduced Lambda expressionsin java version 8.

很高兴知道 java在 java 8 中引入了Lambda 表达式

I was working in android studio. And I noticed that DialogInterface.OnClickListener()was showing underline and became grey colored as below:

我在 android studio 工作。我注意到它DialogInterface.OnClickListener()显示下划线并变成灰色,如下所示:

enter image description here

在此处输入图片说明

So, I did alter + enteron that line and found option to replace the code with Lambda Expressions, as below:

所以,我alter + enter在那行做了,找到了用 Lambda 表达式替换代码的选项,如下所示:

enter image description here

在此处输入图片说明

Which resulted in something below:

这导致了以下内容:

enter image description here

在此处输入图片说明

After running the code it showed me this error:

运行代码后,它向我显示了这个错误:

Error:(160, 69) error: lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)

错误:(160, 69) 错误:-source 1.7 不支持 lambda 表达式(使用 -source 8 或更高版本启用 lambda 表达式)

But I am already using java jdk1.8.0_31. Is there anything else, I need to install ?

但我已经在使用 java 了jdk1.8.0_31。还有什么,我需要安装吗?

Can anyone help me out please..

谁能帮我一下。。

回答by SilentKnight

I don't think Androiddevelopment is ready yet for Java 8. Android Studiosupports the compile level Java 7whether you installed Java 7or Java 8. So, if you want to use new features of Java 8, I think you have to wait Android Studioto support compile level Java 8.

我认为Android开发还没有准备好Java 8Android Studio支持编译级别,Java 7无论您是安装的Java 7还是Java 8. 所以,如果你想使用的新功能Java 8,我认为你必须等待Android Studio支持compile level Java 8

回答by Stepango

You cannot compile Java 8 sources for Android, because Android SDK does not support Java 8 yet.

您无法为 Android 编译 Java 8 源代码,因为 Android SDK 尚不支持 Java 8。

But there is one way to use Lambdas in your code - RetroLambda. This is a little hack that allows you to use some of the features of Java 8 in your project.

但是有一种方法可以在您的代码中使用Lambdas- RetroLambda。这是一个小技巧,允许您在项目中使用 Java 8 的一些功能。