简体   繁体   English

如何抑制匿名 new runnable() 可以替换为 lambda

[英]How can I suppress anonymous new runnable() can be replaced with lambda

I got "anonymous new runnable() can be replaced with lambda" warning with the following code.我使用以下代码收到“匿名新 runnable() 可以用 lambda 替换”警告。

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
sv.post(new Runnable() {

    @Override
    public void run() {
        sv.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

I searched on Google very hard and seems to be re-write using lambda expression...我非常努力地在 Google 上搜索,似乎是使用 lambda 表达式重写的...

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
Runnable test = () -> sv.fullScroll(ScrollView.FOCUS_DOWN);
test.run();

But when I try to run application, Android Studio stops with error as follows:但是当我尝试运行应用程序时,Android Studio 会因错误而停止,如下所示:

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

I can't understand why Android Studio let me use lambda expression even though it can't compile.我不明白为什么 Android Studio 允许我使用 lambda 表达式,即使它无法编译。 Is it a bug?这是一个错误吗?

Also, I tried to use gradle-retrolambda , but it is hard to use for biginner.另外,我尝试使用gradle-retrolambda ,但它很难用于 biginner。

As I can't compile my code, I'm not sure above lambda expresssion is correct or not.由于我无法编译我的代码,我不确定上面的 lambda 表达式是否正确。

In my opinion, IDE should not complain about the code can't be compiled.在我看来,IDE 不应该抱怨代码无法编译。 So I think the warning to use lambda expression should be suppressed.所以我认为应该禁止使用 lambda 表达式的警告。 But I don't know how can it be...但是我不知道怎么会...

Any help is appreciated.任何帮助表示赞赏。

First of all, "anonymous new runnable() can be replaced with lambda" is a warning as you stated. 首先,“anonymous new runnable()可以替换为lambda”是一个警告,如你所说。 While warnings like this are not as serious as compiler errors, you should still understand the reasons for the warning in order to make an informed decision of how to deal with it. 虽然这样的警告不像编译器错误那么严重,但您仍应了解警告的原因,以便做出明智的决定如何处理它。 In this case, the warning comes from the IDE, not the compiler and can be safely ignored. 在这种情况下,警告来自IDE,而不是编译器,可以安全地忽略。 Android Studio should have a setting where you can disable this warning, but I have been unable to find exactly how to do so. Android Studio应该有一个设置,您可以在其中禁用此警告,但我一直无法找到确切的方法。 I would start by clicking on the new Runnable() text in your source code and pressing Alt-Enter to see the quick fix options. 我首先点击源代码中的new Runnable()文本,然后按Alt-Enter查看快速修复选项。

Alternatively if you want to use lambda functions in your code, you need to enable support for Java 8, as the error message that you get states. 或者,如果要在代码中使用lambda函数,则需要启用对Java 8的支持,作为您获得的错误消息。 Note that some Java 8 features are only available if your app targets Kit Kat or later. 请注意,某些Java 8功能仅在您的应用针对Kit Kat或更高版本时可用。 Lambda functions are supported on earlier versions of Android, so you don't have to worry in this case. 早期版本的Android支持Lambda函数,因此在这种情况下您不必担心。 To enable Java 8 for your project, modify your build.gradle file to look similar to the following: 要为项目启用Java 8,请将build.gradle文件修改为类似于以下内容:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

The important part is the compileOptions closure. 重要的部分是compileOptions闭包。 The rest is to give context where this belongs within the file. 剩下的就是给出文件所属的上下文。

Note that I have not compiled and tested this. 请注意,我没有编译和测试这个。 Also, I am unsure if you need both sourceCompatibility and targetCompatibility . 另外,我不确定你是否需要sourceCompatibilitytargetCompatibility I suggest you do some research and experimentation to determine if both are needed in order to compile and run your app on the devices that you wish to target. 我建议你做一些研究和实验,以确定是否需要这两个,以便在你想要定位的设备上编译和运行你的应用程序。

Sources: 资料来源:

How to set -source 1.7 in Android Studio and Gradle 如何在Android Studio和Gradle中设置-source 1.7

Java 8 features into Android Development Java 8功能进入Android开发

Firstly, one must know the difference between compile time errors and run-time errors . 首先,必须知道编译时错误运行时错误之间的区别。

Further reading on Run-time vs Compile-time Errors: 进一步阅读运行时与编译时错误:
Runtime vs Compile time 运行时与编译时间
Warnings and Errors 警告和错误

The reason behind the error you are getting is that it is not Supported in Java < 1.8 versions. 您得到的错误背后的原因是它在Java <1.8版本中不受支持。

The Solution of this problem is that you should change the Java version of your Project to 1.8 此问题的解决方案是您应该将项目的Java版本更改为1.8

Here is procedure of doing that: 这是执行此操作的过程:
Using JDK 7 Or Higher With Android Studio And Eclipse On Mac OSX 在Mac OSX上使用Android Studio和Eclipse使用JDK 7或更高版本

I hope this helps. 我希望这有帮助。

After searching with Google, I have realized that Android does not support JDK8 yet officially. 在用Google搜索后,我意识到Android尚未正式支持JDK8。 See this link 看到这个链接

Though we can JDK8 flavored coding using Retrolambda , (For Android Studio, it is gradle-retrolambda ), or RxJava , they are just a FLAVOR ... 虽然我们可以使用Retrolambda进行JDK8风格的编码,(对于Android Studio,它是gradle-retrolambda ),或者RxJava ,它们只是一个风味 ......

My problem was caused by installing JDK8, instead of should be install JDK7. 我的问题是由安装JDK8引起的,而不是应该安装JDK7。
I thought that it is preferred to install JDK8 because oracle officially supports JDK8 now and have terminated to update JDK7, but it is wrong thought. 我认为安装JDK8是首选,因为oracle现在正式支持JDK8并终止更新JDK7,但这是错误的想法。

After I uninstalled JDK8 and installed JDK7, IDE does not warn to use lambda expression, or does not occur lambda expressions are not supported in -source 1.7 error on compilation. 卸载JDK8并安装JDK7后,IDE不会警告使用lambda表达式,或者不会发生编译时-source 1.7错误中不支持lambda表达式

There seems to be a new problem here in that Android Studio Arctic Fox 2020.3.1 Patch 3 (the current version) doesn't provide a way to disable the Inspection which causes the warning "Anonymous [class] can be replaced with lambda".这里似乎有一个新问题,Android Studio Arctic Fox 2020.3.1 Patch 3(当前版本)没有提供禁用检查的方法,这会导致警告“匿名 [类] 可以用 lambda 替换”。

So if I want to compile without warnings I have to replace all my anonymous classes (I have a lot of them) with lambdas and then I can't run my code with JDK 7.因此,如果我想在没有警告的情况下进行编译,我必须用 lambda 替换我所有的匿名类(我有很多),然后我无法使用 JDK 7 运行我的代码。

There is also an Inspection which causes the warning "Lambda can be replaced with anonymous class", and this one can be disabled.还有一个检查会导致警告“Lambda 可以用匿名类替换”,并且可以禁用这个。

???!!! ???!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM