【问题标题】:on kotlin, textview not initialized outside onCreate method在 kotlin 上,textview 未在 onCreate 方法之外初始化
【发布时间】:2020-02-26 17:57:54
【问题描述】:

我是 Kotlin 的新手,正在尝试实现 MVP 架构,

目前我在 onCreate() 方法之外初始化/设置 textview 的值时遇到问题 这是我的代码

SplashActivity.kt

class SplashActivity : AppCompatActivity(), Splash.ViewInterface {
lateinit var appDetail: AppDetail
lateinit var textTitle: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    textTitle = findViewById(R.id.splash_txt_title) as TextView

    AppSingleton.appContext = applicationContext

    var splashPresentation = SplashPresentation(this)
    splashPresentation.getAppDetailFromService()
}

override fun fetchAppDetailSuccessful(response: SplashServiceObject) {
    AppSingleton.initializeAppDetal(Gson().fromJson(response.json_string, AppDetail::class.java))
    this.appDetail = AppSingleton.appDetail
}

override fun fetchAppDetailFailed(errMsg: String) {
    textTitle.text = errMsg
}
}

SplashPresenter.kt

class SplashPresentation(private val view: Splash.ViewInterface) : Splash.PresentationInterface {

fun getAppDetailFromService() {
    var splashService = SplashService()
    splashService.getAppDetailFromAssets(this)
}

override fun fetchAppDetailFromServiceSuccessful(response: SplashServiceObject) {
    view.fetchAppDetailSuccessful(response)
}

override fun fetchAppDetailFromServiceFailed(errMsg: String) {
    view.fetchAppDetailFailed(errMsg)
}
}

SplashService.kt

 class SplashService {
fun getAppDetailFromAssets(splashPresentation: SplashPresentation) {
    val json_filename = "appdetail.json"
    var jsonResponse: JsonResponse = AppSingleton.commonUtils.fetchJsonFromAssets(json_filename, AppSingleton.appContext!!)
    if (jsonResponse.json_status) {
        var splashServiceObj = SplashServiceObject
        splashServiceObj.json_string = jsonResponse.json_info
        splashServiceObj.response_msg = "JSON Successful fetched."
        splashPresentation.fetchAppDetailFromServiceSuccessful(splashServiceObj)
    } else {
        splashPresentation.fetchAppDetailFromServiceFailed(jsonResponse.json_info)
    }
}
}

在我的 SplashActivity().onCreate() 中,我正在调用访问 Service 的 Presenter,然后 Service 返回一个值给 Presenter,
然后Presenter,返回值给我的SplashActivity的View,其中一个函数是,fetchAppDetailFailed(errMsg)

当我运行应用程序时,它崩溃了,说“textaa”尚未初始化。
回到 Java exp 中,当变量已经在 onCreate() 上实例化时,您可以在活动中的任何位置调用此变量。

提前致谢!

【问题讨论】:

  • 你好@Machee Neraid,请给我发送服务和演示代码。
  • @AnandDiamond,我用演示者和服务更新了我的问题。谢谢!

标签: android kotlin android-mvp


【解决方案1】:

您不能在 Android 上实例化活动。它们由操作系统实例化,操作系统在其上调用生命周期方法。

在 MVP 模式中,View 和 Presenter 都相互引用。由于 Activity(视图)是应用程序的入口点,因此您的 Activity 应该实例化 Presenter 并将其自身的引用传递给 Presenter,以便双向通信。

此外,Presenter 中对 Activity 的引用应指定为 ViewInterface,而不是 Activity,否则你有点违背了使用 MVP 的目的。

class SplashPresentation(private val view: Splash.ViewInterface) : Splash.PresentationInterface {

    //... methods that call functions on view
}

class SplashActivity : AppCompatActivity(), Splash.ViewInterface {

    private val presenter = SplashPresentation(this)

    //...

}

【讨论】:

  • 非常感谢!有用!通过添加 SplashPresentation(private val view: Splash.ViewInterface) 而不是 Context,它现在可以工作了!谢谢!
  • 顺便说一句,你介意检查我的整体代码吗?如果有任何问题或问题?对于像我这样的新手来说,这将是一个很大的帮助!再次感谢!
  • 您可能会考虑将其发布到codereview.stackexchange.com,因为在 cmets 中查看此处并不实际。他们确实要求您的代码在您发布之前确实可以工作,因为它不是用于调试,只是用于批评设计之类的事情。
猜你喜欢
  • 2016-12-08
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多