【问题标题】:Room Kotlin:Entities and Pojos must have a usable public constructorRoom Kotlin:实体和 Pojos 必须有一个可用的公共构造函数
【发布时间】:2017-06-27 08:20:13
【问题描述】:

当我运行我的应用程序时,我收到了跟随错误。

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).
  Tried the following constructors but they failed to match:
  ChatMsg(java.lang.String,int) : [arg0 : null, arg1 : null]
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).
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).
  Tried the following constructors but they failed to match:
  ChatMsg(java.lang.String,int) : [arg0 : null, arg1 : null]
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).
Warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.

这是我的 build.gradle 项目

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()  
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.letv.sarrsdesktop:BlockCanaryExPlugin:0.9.9.2'

    }
}   
allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        maven { url "https://jitpack.io" }
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的 build.gradle 模块

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
kapt {
    generateStubs = true
}
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "cn.sz.cyrus.kotlintest"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions{
            annotationProcessorOptions{
                includeCompileClasspath = true
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    compile 'com.github.GrenderG:Toasty:1.2.5'
    compile 'com.orhanobut:logger:1.15'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.umeng.analytics:analytics:latest.integration'
    compile 'com.github.kittinunf.fuel:fuel-android:1.7.0' //for Android
    compile 'com.github.kittinunf.fuel:fuel-rxjava:1.7.0' //for RxJava support
    compile "android.arch.persistence.room:runtime:1.0.0-alpha3"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3"
    kapt "android.arch.persistence.room:compiler:1.0.0-alpha3"
    compile "android.arch.persistence.room:rxjava2:1.0.0-alpha3" 
}

那是我的 pojo 课

@Entity
data class ChatMsg(var msg: String,
              var to: Int) {

    companion object TO {
        val MASTER = 0
        val ROBOT = 1
    }

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var createTime: Long = System.currentTimeMillis()
    var translMsg: String = ""
}

我用谷歌搜索了我的问题,但结果似乎无法回答我的问题。谁对这个问题有想法。先谢谢了!

【问题讨论】:

  • 构造函数中的msgto 是什么,您将它们用作表中的列还是应该是idcreateTimetranslMsg
  • @OlegOsipenko msg 字段表示关于 ChatMsg 的主要内容。 to 字段表示 ChatMsg 发送到,其值为 MASTER 或 ROBOT。我需要将所有这些字段用作表中的列。
  • 所以只有两列?但我看到一个id 带有@PrimaryKey 注释,所以我认为它也是一个列
  • 我记得所有字段都将被视为列,除了我在这些字段中添加@Ingore 注释。
  • 所以它已经回答了:你在堆栈跟踪中有解释,基本上要么用空参数创建一个空构造函数,要么将所有字段传递给构造函数。 You can have an empty constructor or a constructor whose parameters match the fields (by name and type)

标签: java android kotlin android-room


【解决方案1】:

错误信息说的很清楚:

你可以有一个空的 构造函数或其参数与字段匹配的构造函数(按名称和类型)。

有一点需要注意。您应该使用Annotation Site Target@PrimaryKey 放入正确的位置。

所以你的实体应该有一个空的构造函数:

@Entity
data class ChatMsg(var msg: String, var to: Int) {
    constructor() : this("", UNKNOWN);

    companion object TO {
        val MASTER = 0
        val ROBOT = 1
        private val UNKNOWN = -1; // just use for initializing
    }

    @field: PrimaryKey(autoGenerate = true)
    var id: Int = 0
}

OR 应该有一个与字段匹配的构造函数。

@Entity
data class ChatMsg(
        @field:PrimaryKey(autoGenerate = true) var id: Int, 
        var createTime: Long = System.currentTimeMillis(),
        var translMsg: String = "", 
        var msg: String, var to: Int
) {

    companion object TO {
        val MASTER = 0
        val ROBOT = 1
    }

}

【讨论】:

  • 感谢您的回答。我的问题已经解决了。
  • @Cyrus 一点也不。
  • 关于更多问题,注意:在JVM上,如果主构造函数的所有参数都有默认值,编译器将生成一个额外的无参数构造函数,它将使用默认值。这使得 Kotlin 更容易与 Jackson 或 JPA 等库一起使用,这些库通过无参数构造函数创建类实例。我从here复制过来的。为什么不能生成额外的无参数构造函数?
  • @Cyrus 因为它没有任何 default 参数。
  • 哦,我明白了。谢谢你的耐心 。英文文档对我来说总是有点难理解
猜你喜欢
  • 2017-11-20
  • 2019-05-21
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 2020-09-03
  • 2019-11-03
  • 2017-11-13
  • 2019-09-14
相关资源
最近更新 更多