java - Kotlin 房间 :Entities and Pojos must have a usable public constructor

标签 java android kotlin android-room

当我运行我的应用程序时,出现跟随错误。

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

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

最佳答案

错误信息准确地说:

You can have an empty constructor or a constructor whose parameters match the fields (by name and type).

有一点需要注意。你应该使用 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
    }

}

关于java - Kotlin 房间 :Entities and Pojos must have a usable public constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44775393/

相关文章:

java - Java JNA调用的dll中分配内存错误

android - 如何在 PreferenceActivity 中添加 ImageView?

java - 实现DoublyLinkedList时出现StackOverflow错误

android - 如何通过 kotlin 中的 Intent 传递自定义对象

java - 带有 JDBC 或 Hibernate 的 PostgreSQL,用于使用 CSV 数据的 Web 应用程序

java - 使用 Java Mail API 发送带附件的电子邮件的 Android 应用程序

java - 创建文件夹并存储相机拍摄的图像

android - 如何处理 `is deprecated. Deprecated in Java` ?

java - 在 Java 中查找复杂矩阵的 SVD 矩阵

android - 我的应用在从 Google Play 商店下载时不请求许可。我在我的 list 文件中指定了位置许可