科特林室:实体和Pojos必须有一个可用的公共构造函数(Room Kotlin:Entities and Pojos must have a usable public constructor)

编程入门 行业动态 更新时间:2024-10-14 22:13:40
科特林室:实体和Pojos必须有一个可用的公共构造函数(Room Kotlin:Entities and Pojos must have a usable public constructor)

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

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

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

When I run my app,I got follow error.

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.

That's my build.gradle for project

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 }

That's my build.gradle for module

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

That's my pojo class

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

I have google my problem,but results seems can't answer my question.Who has ideas for this question . Thanks for first!

最满意答案

错误消息准确地说:

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

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

The error message said exactly:

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

One thing should be noted. You should be using Annotation Site Target to put the @PrimaryKey into the correct place.

so your entity should have an empty constructor:

@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 should have a constructor that matches the fields.

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

更多推荐

本文发布于:2023-07-14 14:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   科特   有一个   函数   Pojos

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!