集合不包含与谓词匹配的元素

Raj*_*nan 7 android kapt android-room

我正在使用 Room 库,并且在尝试构建应用程序时收到以下错误消息:

e: [kapt] An exception occurred: java.util.NoSuchElementException: Collection contains no element matching the predicate.
Run Code Online (Sandbox Code Playgroud)

这是更详细的错误消息:

FAILURE: Build failed with an exception
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details
Run Code Online (Sandbox Code Playgroud)

我将代码中的问题隔离在以下某处,尽管我不知道究竟是什么引发了此异常:

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<PomoPlayDatabase>(
                                context.applicationContext,
                                PomoPlayDatabase::class.java,
                                "pomoplay_database").build()
                    }
                }
            }
            return INSTANCE
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
Run Code Online (Sandbox Code Playgroud)

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 Components
    implementation "androidx.room:room-runtime:2.2.3"
    kapt "androidx.room:room-compiler:2.2.3"

    //Testing Components
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}
Run Code Online (Sandbox Code Playgroud)

Car*_*vas 6

就我而言,它发生在我尝试按一个不存在的字段进行过滤时或因为示例

class Items(id:Int, name:String, isNew:Boolean)
Run Code Online (Sandbox Code Playgroud)

所以当我用这样的东西过滤“项目”列表时

var list:Arraylist<Items>
list.filter{it->it.isNew}
Run Code Online (Sandbox Code Playgroud)

如果我的后端没有检索该字段“isNew”,我的代码会抛出异常,这就是我的情况


Raj*_*nan 2

为了追查问题的根源,我取出了项目中的所有文件,然后一次放回一个文件,并将问题隔离到我上面在原始问题中提到的房间数据库类文件。我想也许Category::class注释数组中的属性@Database有问题。

我研究了如何构建该类Category,并将其与其他 Android Room 项目中实体类的构建方式进行了比较。这些项目对实体使用数据类,而我使用常规 POCO 样式结构。因此,我实现了一个 Data 类,现在一切正常。

类别数据类:

import androidx.annotation.NonNull
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "categories_table")
data class Category(@PrimaryKey(autoGenerate = true) @NonNull @ColumnInfo(name = "categoryId") val id: Int, @ColumnInfo(name = "categoryName") val name: String, @ColumnInfo(name = "categoryDesc") val desc: String)
Run Code Online (Sandbox Code Playgroud)