【发布时间】:2019-07-04 11:06:12
【问题描述】:
我正在尝试使用OpenWeatherMap、Kotlin、Retrofit 和MVP 和the clean architecture 制作一个应用程序来检查天气。
应用程序非常简单,只有一个活动,布局根据用户选择的位置显示不同的数据。在启动活动时,这会初始化onCreate中的presenter,并调用启动请求过程的方法。当我带着答案返回我的活动时,我试图在TextView 中显示有关该响应的一些信息,但应用程序崩溃了,因为该视图是null。
我正在使用Kotlin Android Extensions,理论上,它允许我仅使用其 ID 调用视图,而无需使用 findViewById。
我是 Kotlin 的新手,也许我遗漏了一些东西。
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.climaconsulta.R
import com.climaconsulta.user.model.pojos.MainWeather
import com.climaconsulta.user.presenter.MainActivityPresenter
import com.climaconsulta.user.presenter.MainActivityPresenterImpl
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), MainActivityView {
var presenter: MainActivityPresenter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
presenter = MainActivityPresenterImpl()
presenter!!.getMainWeather("London")
}
override fun showCurrentCity() {
presenter!!.getCurrentCity()
}
override fun showMainWeather(mainWeather: MainWeather) {
mainTemperature.text = mainWeather.main!!.temp.toString()
// HERE I TRY TO SET THE TEXT. BUT "mainTemperature" IS NULL
}
override fun showFiveDaysWeather(cityName: String) {
presenter!!.getFiveDaysWheather(cityName)
}
override fun showError(error: String) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
【问题讨论】:
-
向我们展示您的进口产品
-
抱歉...已添加进口!
标签: android kotlin nullpointerexception textview retrofit