本文介绍了Android Room Database忽略问题“尝试了以下构造函数,但它们不匹配"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的实体类别:
@Entity(tableName = "student")
data class Student(
var name: String,
var age: Int,
var gpa: Double,
var isSingle: Boolean,
@PrimaryKey(autoGenerate = true)
var id: Long = 0,
@Ignore //don't create column in database, just for run time use
var isSelected: Boolean = false
)
然后我这样插入(在 androidTest
中测试):
And then I insert like this (tested in androidTest
):
val student = Student("Sam", 27, 3.5, true)
studentDao.insert(student)
在添加 @Ignore
批注之后,它立即给我这个错误:
It gives me this error right after I added the @Ignore
annotation:
C:\Android Project\RoomTest\app\build\tmp\kapt3\stubs\debug\com\example\roomtest\database\Student.java:7: ����: 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 Student {
^
Tried the following constructors but they failed to match:
Student(java.lang.String,int,double,boolean,boolean,long) -> [param:name ->
matched field:name, param:age -> matched field:age, param:gpa -> matched
field:gpa, param:isSingle -> matched field:isSingle, param:isSelected ->
matched field:unmatched, param:id -> matched field:id][WARN] Incremental
annotation processing requested, but support is disabled because the
following processors are not incremental: androidx.room.RoomProcessor
(DYNAMIC).
推荐答案
由于Room仍在编译期间生成Java类,并且问题在于默认值参数,因此请尝试使用 @ JvmOverloads 用于构造函数:
Since Room still generates Java-classes during compile and the problem is with default-valued parameter, try to use @JvmOverloads for constructor:
@Entity(tableName = "student")
data class Student @JvmOverloads constructor(
var name: String,
.....
更新
有趣的是,此处为文档没有提及这种情况的特殊待遇.并且此问题已存在,可以修复此文档.
It's interesting that here in documentation there is no mentioning of special treatment to this case. And this issue already exists to fix this documentation.
来自此问题,该问题本身的结论是:
From this issue as for the problem itself conclusion is:
这篇关于Android Room Database忽略问题“尝试了以下构造函数,但它们不匹配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!