错误:包含非法的最终字段-Kotlin
我正在使用带有Realm的Kotlin数据类,Gson注释用于从服务器获取数据。
问题:当我在android studio中运行项目时,会出现以下错误
Error:Class "VenderConfig" contains illegal final field "name".
我正在学习Kotlin,所以对此没有太多的想法。
我的VenderConfig
类是:
@RealmClass class VenderConfig( @SerializedName("name") val name: String? = null, @SerializedName("website") val wb_url: String? = null, @SerializedName("icon") val icon: String? = null, @SerializedName("logo") val logo: String? = null, @SerializedName("description") val description: String? = null, @PrimaryKey @SerializedName("id") val id: Int? = null ) : RealmObject() { }
我也尝试打开关键字与字段并删除数据关键字也,但它没有解决问题。
你应该使用var
关键字来声明可变属性。 val
代表不可变(最终)的。
var name: String? = null name = "Kotlin" // OK val immutableName: String? = null immutableName = "Java" // won't compile, val cannot be reassigned
有关更多信息: 属性和字段
请注意,我对Realm不熟悉,这可能无法解决您的问题。
班上课不应该开放吗? (我也是 ;)
@ Realm-database:realm-java-3.1.0.zip \ examples \ kotlinExample \ src \ main \ kotlin \ io \ realm \ examples \ kotlin \ model \ Person.kt
package io.realm.examples.kotlin.model import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.Ignore import io.realm.annotations.PrimaryKey // ... // Furthermore, the class and all of the properties // must be annotated with open // (Kotlin classes and methods are final by default). // open class Person( ... ) : RealmObject() { // The Kotlin compiler generates standard getters and setters. // Realm will overload them and code inside them is ignored. // So if you prefer you can also just have empty abstract methods. }