[英]How to use lambda expression in android?
我已經嘗試了所有可以從StackOverflow
找到的方法來解決這個問題,但沒有一個成功。
奇怪的是:我可以使用 lambda,例如
activity.runOnUiThread(() -> {
activity.startActivity(intent);
});
像這樣流
activity.getPackageManager().getInstalledPackages(0).stream().collect(Collectors.toList())
但這會失敗:
activity.getPackageManager()
.getInstalledPackages(0)
.stream()
.filter(e -> e.packageName.contains("com"))
.collect(Collectors.toList())
plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.mylearn.simplesender"
minSdk 25
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.2'
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.78'
implementation group: 'com.google.guava', name: 'guava', version: '31.0.1-android'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
而且,這是我在Android Studio
Gradle 設置
這是我在Android Studio
編譯設置
它顯示了一個不可讀的錯誤,當我在觀察者中調試這個表達式時顯示:
Compilation failed:
-source 1.6 中不支持 lambda 表達式
(請使用 -source 8 或更高版本以啟用 lambda 表達式)
這意味着我使用的是源版本 1.6,它不支持 lambda 表達式並告訴我使用源 8 或更高版本來啟用 lambda 表達式。
但是我已經在 Java 11 中了,這怎么會發生呢?
ts 因為也許您正在使用 Java 7 而實際上, Retrolambda
是一個我們可以與Java 8 lambda expressions.
一起使用的庫Java 8 lambda expressions.
將以下內容添加到您的**project's main build.gradle**
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
然后將此添加到**your application module's build.gradle**
apply plugin: 'me.tatarka.retrolambda'
然后我們需要將這些行添加到**your application module's build.gradle**
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
完成后,您可以像下面這樣使用它:-
例如。)單擊帶有 Retrolambda 的按鈕。
// RETROLAMBDA WAY
clickMeBtn.setOnClickListener(view ->
Toast.makeText(MainActivity.this,
"This is the way to click a button to make a toast with RetroLambda !", Toast.LENGTH_LONG).show());
匿名類 new View.OnClickListener()
可以替換為lambda .
轉到應用程序級別 build.gradle>
然后將此代碼放在 android {} 塊中
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
在此之后你的代碼看起來像這樣
android {
compileSdkVersion 31
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "in.chikuai.example"
minSdkVersion 19
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
最近,我根據官方文檔通過更改我的 build.gradle 文件解決了問題。 現在我的 build.gradle 文件是:
plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.infiai.simplesender"
minSdk 25
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.2'
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.78'
implementation group: 'com.google.guava', name: 'guava', version: '31.0.1-android'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
我現在可以同時使用流和 lambda,但是當表達式在監視窗口中時,我不知道如何在調試模式下使用它,即使我的代碼中的相同表達式運行成功,它仍然會拋出異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.