【发布时间】:2021-02-23 21:04:12
【问题描述】:
在构建我的应用程序时,我收到了两个实体类的错误。它基本上是说它找不到我给的构造函数的设置器。最初我有 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,但在接口中将其保留为 val。
【问题讨论】:
标签: android kotlin android-room