I got "anonymous new runnable() can be replaced with lambda" warning with the following code.
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...
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:
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. Is it a bug?
Also, I tried to use gradle-retrolambda, but it is hard to use for biginner.
As I can't compile my code, I'm not sure above lambda expresssion is correct or not.
In my opinion, IDE should not complain about the code can't be compiled. So I think the warning to use lambda expression should be suppressed. 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. 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.
First, here’s the Java 8 lambda syntax for a Runnable, where I create a Runnable and pass it to a Thread: Runnable runnable = () -> { // your code here ... }; Thread t = new Thread (runnable); t.start (); Here’s the Java 8 Thread lambda syntax (without a Runnable ):
If you can’t use Java 8 lambdas — or don’t want to — here’s the pre-lambda thread syntax using a Runnable: Here’s the old Thread syntax, using the anonymous class approach: You can also create a class to extend a Thread and then run it, like this:
They can be used to represent one method interface (also known as functional interface) in a clear and concise way using an expression in the form of: In this article, we see how Lambda expressions can simplify the creation of a new thread. 1. Create a Java thread via Runnable using Classic Code 2.
First of all, "anonymous new runnable() can be replaced with lambda" is a warning as you stated. 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. Android Studio should have a setting where you can disable this warning, but I have been unable to find exactly how to do so. I would start by clicking on the new Runnable()
text in your source code and pressing Alt-Enter to see the quick fix options.
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. Note that some Java 8 features are only available if your app targets Kit Kat or later. Lambda functions are supported on earlier versions of Android, so you don't have to worry in this case. To enable Java 8 for your project, modify your build.gradle
file to look similar to the following:
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. 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
. 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
Java 8 features into Android Development
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.
The Solution of this problem is that you should change the Java version of your Project to 1.8
Here is procedure of doing that:
Using JDK 7 Or Higher With Android Studio And Eclipse On Mac OSX
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With