Collection contains no element matching the predicate

8 浏览
0 Comments

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'
}

0
0 Comments

原因:出现(Collection contains no element matching the predicate)的问题是因为在对集合进行过滤时,使用了一个不存在的字段作为过滤条件。

解决方法:检查代码中的过滤条件是否使用了正确存在的字段。在这个例子中,可以通过检查"Items"类中是否有名为"isNew"的字段来解决这个问题。如果"isNew"字段不存在,可以选择使用正确存在的字段来进行过滤。

0
0 Comments

问题的原因是在类中存在一个空构造函数。解决方法是删除空构造函数或者确保该构造函数的参数与字段的名称和类型匹配。

有时候这个空构造函数是必要的,否则会出现这个错误。实体类和POJO类必须有一个可用的公共构造函数,可以是空构造函数,也可以是参数与字段匹配的构造函数。

尽管我们不理解这个原因,但这肯定是我们的用法问题。这并不意味着Android Studio应该在编译错误的行号上保持沉默,这就像一个捕鼠器,指向正确的提示。

0
0 Comments

问题出现的原因是在数据库类文件中使用了错误的注解属性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)

0