gpt4 book ai didi

android - ROOM 构造函数在构建时无法匹配

转载 作者:行者123 更新时间:2023-12-05 00:07:53 33 4
gpt4 key购买 nike

在构建我的应用程序时,我遇到了两个实体类的错误。它基本上是说它找不到我提供的构造函数的 setter 。最初我有 vals,我将它们转换为 vars,这似乎解决了这个问题。

但我不喜欢这个修复...文档在如何构建实体的示例中使用了 vals。那么为什么它对我定义的某些实体不起作用?我觉得我正在做的编码很容易出现某种错误,因为我只是在包扎问题而不是实际修复它。

https://developer.android.com/training/data-storage/room/defining-data

data class DirectMessage(
@PrimaryKey
@ColumnInfo(name = "dm_id") override val objectId: String,

//Author of message
@Ignore override val author: User,

//Recipient of message, usually user of the app
@Ignore override val recipient: User,

//Content of message
override val content: String,
//Date of creation
override val timestamp: Long

) : DirectMessage {

//Place identifier into database instead of Entity
@ColumnInfo(name = "author") val _author : String = author.uid
@ColumnInfo(name = "recipient") val _recipient : String = recipient.uid

/**
* Get author's user thumbnail
*/
override fun getThumbnail() {
TODO("Not yet implemented")
}

}

@Entity
data class Comment (

@PrimaryKey
@ColumnInfo(name = "cid") override val objectId: String,

//Author of the comment
@Ignore override val author: User,

//Moment ID this comment is attached to
override var momentId: String,

//Time comment was created
override val timestamp: Long,

//Content of the comment
override var content: String

) : Comment {

//Place identifier into database instead of User entity
@ColumnInfo(name = "author") val _author = author.uid

/**
* Get thumbnail of User object
*/
override fun getThumbnail() {
TODO("Not yet implemented")
}

}

interface Comment : Message {

val momentId : String
}

public final class Comment implements com.example.barrechat192.data.entities.Comment {
^
Tried the following constructors but they failed to match:
Comment(java.lang.String,com.example.barrechat192.data.entities.User,java.lang.String,long,java.lang.String) -> [param:objectId -> matched field:objectId, param:author -> matched field:unmatched, param:momentId -> matched field:momentId, param:timestamp -> matched field:timestamp, param:content -> matched field:content]C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\Comment.java:13: error: Cannot find setter for field.
private final java.lang.String _author = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\Comment.java:17: error: Cannot find setter for field.
private final java.lang.String objectId = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\Comment.java:23: error: Cannot find setter for field.
private final long timestamp = 0L;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:7: 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).
public final class DirectMessage implements com.example.barrechat192.data.entities.DirectMessage {
^
Tried the following constructors but they failed to match:
DirectMessage(java.lang.String,com.example.barrechat192.data.entities.User,com.example.barrechat192.data.entities.User,java.lang.String,long) -> [param:objectId -> matched field:objectId, param:author -> matched field:unmatched, param:recipient -> matched field:unmatched, param:content -> matched field:content, param:timestamp -> matched field:timestamp]C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:10: error: Cannot find setter for field.
private final java.lang.String _author = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:13: error: Cannot find setter for field.
private final java.lang.String _recipient = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:17: error: Cannot find setter for field.
private final java.lang.String objectId = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:25: error: Cannot find setter for field.
private final java.lang.String content = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\messages\DirectMessage.java:26: error: Cannot find setter for field.
private final long timestamp = 0L;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\implementations\mapobjects\MapObject.java:10: error: The name "map_objects" is used by multiple entities or views: com.example.barrechat192.data.entities.implementations.mapobjects.MapObject, com.example.barrechat192.data.entities.implementations.mapobjects.MapObject
public final class MapObject implements com.example.barrechat192.data.entities.MapObject {
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\AppDatabase.java:8: error: The name "map_objects" is used by multiple entities or views: com.example.barrechat192.data.entities.implementations.mapobjects.MapObject, com.example.barrechat192.data.entities.implementations.mapobjects.MapObject
public abstract class AppDatabase extends androidx.room.RoomDatabase {
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\geocache\GeoTableWithMapObjects.java:12: error: Cannot find the child entity column `geohash` in com.example.barrechat192.data.entities.implementations.mapobjects.MapObject. Options: objectId, timestamp, thumbnailUrl, thumbnailPath, objectType, local, views, viewed, geoHash, latitude, longitude
private final java.util.List<com.example.barrechat192.data.entities.implementations.mapobjects.MapObject> mapObjects = null;
^C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\data\entities\geocache\GeoTableWithMapObjects.java:6: 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).

我也不明白为什么我可以在Kotlin的子类中继承一个val并把它改成一个var。这似乎也使代码工作,将实体类中的 val 更改为 var,但在接口(interface)中将其保留为 val。

最佳答案

您好,您已经创建了构造函数,您在创建时会使用这些构造函数。您应该创建构造函数,将参数传递给所有数据库列。 Room 正在尝试创建一个实例,但找不到这些映射字段的 setter

关于android - ROOM 构造函数在构建时无法匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64794087/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com