【发布时间】:2019-04-15 10:08:15
【问题描述】:
在 kotlin 成为 android 的第一语言之后,我就全身心投入其中。随着这些天的一点点进步,我一直在将我现有的知识迁移到 kotlin。最近,我正在学习如何在一个虚拟项目中使用 GSON、Retrofit 和 kotlin。
这里CurrentWeather是在视图中显示数据的模型
data class CurrentWeather(
val latitude: Double,
val longitude: Double,
val placeName: String,
val temperature: Float,
val maxTemperature: Float,
val minTemperature: Float,
val windSpeed: Float,
val windDirection: Float,
val weatherType: String,
val weatherDescription: String,
val icon: String,
val timestamp: Instant)
class Current 负责将 JSON 解析为 POJO 类,就像我过去所做的一样,但今天使用 kotlin 看起来有点不同
data class Current(@SerializedName("coord") val location: Location,
@SerializedName("weather") val weather: List<Weather>,
@SerializedName("main") val temperatureAndPressure: TemperatureAndPressure,
@SerializedName("wind") val wind: Wind,
@SerializedName("dt") val timeStamp: Long,
@SerializedName("name") val placeName: String) {
val time: Instant by fastLazy { Instant.ofEpochSecond(timeStamp) }
val currentWeather = CurrentWeather(location.latitude,
location.longitude,
placeName,
temperatureAndPressure.temperature,
temperatureAndPressure.maxTemperature,
temperatureAndPressure.minTemperature,
wind.windSpeed ?: 0f,
wind.windAngle ?: 0f,
weather[0].main,
weather[0].description,
weather[0].icon,
time)
}
即使我从改造中获得了成功的响应(我已经检查了成员变量;例如位置:位置、天气:列表、温度和压力:温度和压力等。但是,我收到了这个错误。
2018-11-12 21:04:07.455 9948-9948/bus.green.fivedayweather E/AndroidRuntime: FATAL EXCEPTION: main 进程:bus.green.fivedayweather,PID:9948 java.lang.IllegalArgumentException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数 p1 在 bus.green.fivedayweather.ui.CurrentWeatherFragment$retrieveForecast$1.invoke(未知来源:6) 在 bus.green.fivedayweather.ui.CurrentWeatherFragment$retrieveForecast$1.invoke(CurrentWeatherFragment.kt:20) 在 bus.green.fivedayweather.net.OpenWeatherMapProvider$RetrofitCallbackWrapper.onResponse(OpenWeatherMapProvider.kt:62)
我在解析时做错了吗?
【问题讨论】:
-
你能发布你的方法
retrieveForecast,它叫什么?这似乎是堆栈跟踪所指的