我的数据类是这样的代码:
import android.content.Context
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class Rate(
@field:Json(name = "name") val code: String,
@field:Json(name = "value") val value: Float
) {
fun getName(context: Context): String? {
return when (code) {
"AED" -> context.getString(R.string.name_aed)
"AFN" -> context.getString(R.string.name_afn)
"ZAR" -> context.getString(R.string.name_zar)
"ZMW" -> context.getString(R.string.name_zmw)
"ZWL" -> context.getString(R.string.name_zwl)
else -> null
}
}
}
摇篮:
plugins {
id 'com.android.application'
id 'kotlin-android'
//apply plugin: 'kotlin-kapt'
//id "org.jetbrains.kotlin.kapt"
id 'kotlin-kapt'
id 'kotlin-android-extensions'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
/*buildFeatures {
viewBinding = true
}*/
...
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation("com.squareup.moshi:moshi:1.12.0")
implementation("com.squareup.moshi:moshi-kotlin:1.12.0")
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
}
当我删除数据类时,没有错误。如您所见,this 答案无济于事。如何解决这个问题?没有在任何帖子中找到解决方案。我不确定代码有什么问题。你有过类似的经历吗?