当前位置: 首页 > 知识库问答 >
问题:

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

壤驷向明
2023-03-14
e: [kapt] An exception occurred: java.util.NoSuchElementException: Collection contains no element matching the predicate.

以下是更详细的错误消息:

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

我将代码中的问题隔离在下面的某个地方,尽管我不知道抛出这个异常的具体原因:

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

}

共有1个答案

锺威
2023-03-14

不需要创建单独的数据类。只需确保其serializable

在花了一天时间处理这个错误之后,我发现问题是@Entity类中存在一个空构造函数。

constructor() {}

并不是总是不存在这个构造函数。我有另一个@Entity类,它是非常需要的,否则它会失败,出现以下错误

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
 类似资料:
  • 我有一个包含有4个参数(x,y,iD和myType)的对象的ArrayList。我想验证这个ArrayList中是否有具有特定坐标的对象,独立于它们的iD和myType参数。我想使用但当对象只有一个参数时。 以下是全部代码: 例如,如果我想验证是否有一个具有坐标(3.5,4.5)的对象,我应该如何继续?有没有简单的方法? 谢谢你的帮忙

  • 我知道如何通过谓词查找列表的第一个元素:Java8通过谓词查找第一个元素 有没有一种简单的方法可以得到那个元素的索引? 编辑:链接的问题确实提供了答案,但我在搜索时找不到它,因为它的措辞。因此,我宁愿保留这个问题。

  • 问题内容: 我想要一种惯用的方式来找到与谓词匹配的列表中的第一个元素。 当前代码非常丑陋: 我已经考虑过将其更改为: 但是必须有一些更优雅的方法……如果返回一个值而不是没有找到匹配项引发异常,那将是一个很好的选择。 我知道我可以像这样定义一个函数: 但是,如果已经有内置的插件开始用这样的实用函数填充代码,这是很鸡肋的(人们可能不会注意到它们已经在那里,因此随着时间的推移它们会不断重复出现)。 问题

  • 问题内容: 我知道可以匹配一个单词,然后使用其他工具(例如grep -v)将匹配项反转。但是,是否可以hede使用正则表达式来匹配不包含特定单词的行? 输入: 码: 所需的输出: 问题答案: 正则表达式不支持逆匹配的说法并不完全正确。您可以使用否定环顾模仿此行为: 上面的正则表达式将匹配任何不包含(sub)字符串’hede’的字符串或没有换行符的行。如前所述,这是不是正则表达式是“好”的(或应该做

  • 来自< code>$elementMatch的MongoDB文档: $elemMatch运算符将包含数组字段的文档与至少一个与所有指定查询条件匹配的元素匹配。 但是,如何将包含数组字段的文档与与查询匹配的所有元素进行匹配? 例如,我有这样的文档: 我需要匹配所有具有所有 如果我使用以下查询: 该文档将被匹配,因为至少有一个价格 我也试过这个,但它似乎不工作: 有没有办法排除查看所有价格而不是至少一

  • 我有一个集合,其中每个文档都有一些公共数据,如用户名和字符级别,但也有一个私有子集合,它有字符的黄金量。 现在我可以查询特定文档的characters collection,但是子collection数据不会返回,所以我需要执行二次查询来检索它。