Collection contains no element matching the predicate
Collection contains no element matching the predicate
我正在使用Room库,在构建应用程序时遇到以下错误消息:
e: [kapt] 发生异常:java.util.NoSuchElementException: 集合中没有与谓词匹配的元素。
这是一个更详细的错误消息:
失败:构建失败,出现异常
* 出现了什么问题:
任务':app:kaptDebugKotlin'执行失败。
> 编译错误。有关更多详细信息,请参阅日志
我在代码中确定了问题出现在以下位置,尽管我不知道具体是什么导致了这个异常:
package com.example.pomoplay
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = [(Category::class)], version = 1)
abstract class PomoPlayDatabase: RoomDatabase() {
abstract fun categoryDao(): CategoryDao
companion object {
private var INSTANCE: PomoPlayDatabase? = null
internal fun getDatabase(context: Context): PomoPlayDatabase?
{
if (INSTANCE == null) {
synchronized(PomoPlayDatabase::class.java) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder
context.applicationContext,
PomoPlayDatabase::class.java,
"pomoplay_database").build()
}
}
}
return INSTANCE
}
}
}
Gradle.build - 项目
buildscript { ext.kotlin_version = '1.3.61' repositories { google() jcenter() } dependencies { classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0' classpath 'com.android.tools.build:gradle:3.5.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
Gradle.build - 模块
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: "androidx.navigation.safeargs.kotlin" apply plugin: 'kotlin-kapt' android { packagingOptions { exclude 'META-INF/atomicfu.kotlin_module' } compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.example.pomoplay" minSdkVersion 23 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.core:core-ktx:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0' implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0' implementation 'androidx.navigation:navigation-ui-ktx:2.1.0' //Room组件 implementation "androidx.room:room-runtime:2.2.3" kapt "androidx.room:room-compiler:2.2.3" //测试组件 testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
问题出现的原因是在数据库类文件中使用了错误的注解属性Category::class
。解决方法是将实体类Category
改为使用Data类的结构来构建。
为了追踪问题的来源,我将项目中的所有文件都移除,然后逐个添加文件,并将问题隔离到了我在原始问题中提到的房间数据库类文件中。我认为可能是注解数组中的Category::class
属性存在问题。
我查看了我如何构建Category
类,并将其与其他Android Room项目中构建实体类的方式进行了比较。那些项目使用了Data类作为实体类,而我使用了常规的POCO风格结构。因此,我改为实现一个Data类,现在一切正常。
Category数据类:
import androidx.annotation.NonNull
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
(tableName = "categories_table")
data class Category((autoGenerate = true) (name = "categoryId") val id: Int, (name = "categoryName") val name: String, (name = "categoryDesc") val desc: String)