集合不包含与谓词匹配的元素 [英] Collection contains no element matching the predicate

查看:746
本文介绍了集合不包含与谓词匹配的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Room library, and I'm getting the following error message when I try to build the app:

e: [kapt] An exception occurred: java.util.NoSuchElementException: Collection contains no element matching the predicate.

这是更详细的错误消息:

Here is a more detailed error message:

FAILURE: Build failed with an exception
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details

我将代码中的问题隔离在以下位置,尽管我不知道到底是什么引发了该异常:

I isolated the problem in the code to be somewhere in the following, although I don't know what exactly is throwing this exception:

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

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

}

推荐答案

无需创建单独的Data类.只需确保其Serializable

No need to create separate Data class. Just make sure its Serializable

花了一天的时间解决此错误后,我发现问题是@Entity类中存在空的构造函数.

After spending a day for this error, I identified the issue to be presence of an empty constructor in the @Entity class.

constructor() {}

并非总是应该缺少此构造函数. 我还有一个非常需要它的@Entity类,否则它会因该错误而失败

Its not that always this constructor should be absent. I have another @Entity class where it is very much needed or else it fails with this error

Entities and POJOs must have a usable public constructor. 
You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class Question implements java.io.Serializable

不了解其背后的原因,但必须是我们使用的方式. 这并不意味着Android Studio应该在编译错误的行号上保持沉默.感觉就像一个老鼠陷阱

Not understood the reasoning behind it, but must be way of our usage. That shouldn't mean Android Studio should go silent on Line number of compilation error. It felt like a mouse trap

这篇关于集合不包含与谓词匹配的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆