Raj*_*Raj 2 java android android-api-levels
使用 Comparator 时出现 api 级别错误,但是当我添加 @RequiresApi(N) 注释时,错误得到解决。

问题是添加 @RequiresApi(N) 注释后,此应用程序无法在低于 24 的 api 级别运行,我在 API 23 手机上收到此错误 -
Process: com.example.chatapp, PID: 13778
java.lang.NoClassDefFoundError: adapters.chatPageAdapter$$ExternalSyntheticLambda0
at adapters.chatPageAdapter.onBindViewHolder(chatPageAdapter.java:59)
at adapters.chatPageAdapter.onBindViewHolder(chatPageAdapter.java:25)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
Run Code Online (Sandbox Code Playgroud)
那么,有什么方法可以使用这个方法,甚至可以在 23 以下的 api 上运行应用程序吗?
构建.Gradle -
android {
compileSdk 30
buildFeatures {
viewBinding true
}
defaultConfig {
applicationId "com.example.chatapp"
minSdk 23
targetSdk 30
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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以启用脱糖- 它将允许较低的 api 版本也使用最新的 java8 功能。
在您的应用程序模块中build.gradle,添加coreLibraryDesugaringEnabled
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1766 次 |
| 最近记录: |