Woo*_*gie 12 android kotlin android-room
我一直在使用Room,我遇到了阻塞问题.我已经完成并修复了Room库中的所有编译时检查,但现在遇到以下错误:
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).
Run Code Online (Sandbox Code Playgroud)
这在编译时出现两次,没有证据表明它来自哪个类,但我能够弄清楚(通过从数据库中删除类)这是其中一个文件.我假设它与主键是一个字符串而不是一个Int(这是使用它的两个类之一),但文档中没有任何内容表明问题是什么,事实上文档显示该字符串是有效的主键.
@Entity(tableName = "inspections")
data class Inspection(
@SerializedName("id")
var id: Int = 0,
...
// Rest of code left off for brevity, found to not be related to the issue.
Run Code Online (Sandbox Code Playgroud)
我尝试了一些尝试来解决这个问题.
Room cannot pick a constructor since multiple constructors are suitable
- 默认构造函数上的Ignore注释会解决这个问题.)这是让我最困惑的部分 - 删除这个说"多个构造函数是有效的",保留它说"没有构造函数是有效的".更新:从我的项目中添加一些更相关的代码片段.
的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
.....
implementation 'android.arch.persistence.room:runtime:1.0.0-alpha9-1'
implementation 'android.arch.persistence.room:rxjava2:1.0.0-alpha9-1'
kapt 'android.arch.persistence.room:compiler:1.0.0-alpha9-1'
Run Code Online (Sandbox Code Playgroud)
数据库类
@Database(entities =
arrayOf(Account::class, Category::class,
Inspection::class, InspectionForm::class,
InspectionFormItem::class, InspectionFormsStructure::class,
InspectionItemPhoto::class,
InspectionItem::class, LineItem::class,
LocalPhoto::class, Rating::class,
Structure::class, SupervisoryZone::class,
Upload::class, User::class),
version = 16)
@TypeConverters(Converters::class)
abstract class OrangeDatabase : RoomDatabase() {
abstract fun inspectionDao(): InspectionDao
abstract fun localDao(): LocalDao
abstract fun ratingsDao(): RatingsDao
abstract fun structureZoneDao(): StructureZoneDao
abstract fun userAccountDao(): UserAccountDao
}
Run Code Online (Sandbox Code Playgroud)
转换器
class Converters {
@TypeConverter
fun fromTimestamp(value: Long?): Date? {
return if (value == null) Date() else Date(value)
}
@TypeConverter
fun dateToTimestamp(date: Date?): Long? {
return date?.time ?: 0
}
@TypeConverter
fun fromStringToArray(value: String?): Array<String>? {
return value?.split(",")?.toTypedArray() ?: arrayOf()
}
@TypeConverter
fun stringToStringArray(strings: Array<String>?): String? {
return strings?.joinToString(",") ?: ""
}
}
Run Code Online (Sandbox Code Playgroud)
另一个数据类
@Entity(tableName = "users")
data class User(
@PrimaryKey
@SerializedName("id")
var id: Int = 0,
...
// Rest of code left off for brevity, found to not be related to the issue.
Run Code Online (Sandbox Code Playgroud)
UserPermissions类:
data class UserPermissions(
@SerializedName("id")
var pid: Int = 0,
...
// Rest of code left off for brevity, found to not be related to the issue.
Run Code Online (Sandbox Code Playgroud)
Ema*_*l S 16
在你的情况下,问题是,如果你有可空值,Kotlin将为每个可能的构造函数生成几个构造函数.
这意味着您必须定义默认构造函数并使用默认值填充它.
如果你想要另一个应该被忽略的,你应该确保使用带有所有这些参数的父构造函数.
例:
@Entity(tableName = "inspections")
data class Inspection(
@SerializedName("id")
var id: Int = 0,
@PrimaryKey
@SerializedName("guid")
var guid: String = "",
@SerializedName("score")
var score: Double = 0.0,
@SerializedName("notification_sent_at")
var notificationSentAt: Date = Date(),
var wasUploaded: Boolean = false) {
@Ignore
constructor() : this(0, "", 0.0, Date(), false)
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,"引擎盖下"只会生成两个构造函数.如果您具有可空值,则可以使用所有可能的构造函数.
例:
data class Test(var id: Int = 0, var testString: String? = null, var testBool : Boolean? = null) {
constructor(0)
}
Run Code Online (Sandbox Code Playgroud)
生成
constructor(var id:Int)
constructor() : this(0)
constructor(var id:Int, var testString: String)
constructor(var id:Int, var testBool: Boolean)
constructor(var id:Int, var testString: String, var testBool : Boolean)
// .. and so on
Run Code Online (Sandbox Code Playgroud)
既然您正在寻找官方文档,那么您可能需要查看Overloads Generation.
在测试了你的课程后,我在另一篇文章中找到了完美无缺的工作,你必须检查你是否apply plugin: 'kotlin-kapt'
在Gradle中使用过.
仔细检查您的Date类是否有效的类型转换器.我很久以前写过这个问题.
重新编码上面的东西后,通过添加类似的UserPermissions类工作得很好:
data class UserPermissions(var permissionid: String)
Run Code Online (Sandbox Code Playgroud)
编辑:使用UserPermission类后,一切正常.如果使用正确的导入(例如util.Date而不是sql.Date),请注意.
另一个问题是你使用一个旧的非常多的房间库.
当前版本(写这篇文章时)是
implementation "android.arch.persistence.room:runtime:1.0.0-beta2"
kapt "android.arch.persistence.room:compiler:1.0.0-beta2"
implementation "android.arch.persistence.room:rxjava2:1.0.0-beta2"
Run Code Online (Sandbox Code Playgroud)
我很久以前写过一个问题
这个问题非常难以调试,也很难重现,但我发现了这个问题。我正在使用一个@Embedded
对象,但输入的结果实际上是List
该对象的一个。这给自动嵌入任务带来了麻烦,并且没有可以为其编写的完美转换器。
@SerializedName("range_choices")
@Embedded
var rangeChoices: List<RangeChoice>? = null,
Run Code Online (Sandbox Code Playgroud)
我必须用 来注释它@Ignore
,相反,我将把这个列表的结果保存到它自己的表中,现在是新表range_choices
。
归档时间: |
|
查看次数: |
8247 次 |
最近记录: |