简体   繁体   English

我无法解决这个错误“lateinit property dataBinding has not been initialized”

[英]I cant solve this error "lateinit property dataBinding has not been initialized"

I have base activity< T: ViewDataBinding, VM: ViewModel > extends AppCompatActivity()我有基本活动 < T: ViewDataBinding, VM: ViewModel > extends AppCompatActivity()

and i initialize view binding and view model but when run the app i get this error " lateinit property dataBinding has not been initialized "我初始化视图绑定并查看 model 但是在运行应用程序时出现此错误“ lateinit property dataBinding has not been initialized

I don't know what I miss or what the wrong我不知道我错过了什么或出了什么问题

Below is Base Activity Code以下是基本活动代码

open abstract class BaseActivity<T : ViewDataBinding , VM : ViewModel> : AppCompatActivity() {

    lateinit var dataBinding : T
    lateinit var viewModel : VM

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

        dataBinding = getViewBinding()
        setContentView(dataBinding.root)
        viewModel  = generateViewModel()
    }

    abstract fun getViewBinding(): T
    abstract fun generateViewModel(): VM


and this My HomeActivity和这个我的家庭活动

class HomeActivity : BaseActivity<ActivityHomeBinding, HomeViewModel>() {

    override fun getViewBinding(): ActivityHomeBinding = ActivityHomeBinding.inflate(layoutInflater)


    override fun generateViewModel(): HomeViewModel {
        return ViewModelProvider(this).get(HomeViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding.vm = viewModel
          }
}

this is the error message这是错误信息

When you are calling super.onCreate(savedInstanceState) in your HomeActivity , the onCreate from android's Activity is called, but not the onCreate from BaseActivity - because it expected second param persistentState .当您在HomeActivity中调用super.onCreate(savedInstanceState)时,将调用来自 android ActivityonCreate ,但不会调用来自BaseActivityonCreate - 因为它需要第二个参数persistentState

So you can do this options to fix the issue:所以你可以做这个选项来解决这个问题:

  1. call super method with 2 params in your HomeActivityHomeActivity中使用 2 个参数调用 super 方法

    class HomeActivity... {... override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { super.onCreate(savedInstanceState, persistentState) dataBinding.vm = viewModel }

OR或者

  1. use onCreate with one param in your BaseActivity在您的BaseActivity中使用带有一个参数的onCreate

     open abstract class BaseActivity<T: ViewDataBinding, VM: ViewModel>: AppCompatActivity() { lateinit var dataBinding: T lateinit var viewModel: VM override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)...

Because in your base class the onCreate() with 2 parameters is not going to get called during the activity's lifecycles.因为在您的基础 class 中,带有 2 个参数的onCreate()不会在活动的生命周期内被调用。 And in your subclass you override the onCreate() with a parameter.在您的子类中,您使用参数覆盖onCreate()

Just simple change your base class to override the onCreate() with a parameter to fix the problem.只需简单地更改您的基础 class 以使用参数覆盖onCreate()即可解决问题。 And the other thing is you implement these class the java's way.另一件事是您以 Java 的方式实现这些 class。

You can just make it better this way:你可以通过这种方式让它变得更好:

BaseClass基类

abstract class BaseActivity<T : ViewDataBinding , VM : ViewModel> : AppCompatActivity() {

    protected abstract val dataBinding : T
    protected abstract val viewModel : VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(dataBinding.root)
    }
}

Subclass:子类:

class HomeActivity : BaseActivity<ActivityHomeBinding, HomeViewModel>() {

    override val viewModel get() = ViewModelProvider(this).get(HomeViewModel::class.java)

    override val dataBinding get() = ActivityHomeBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding.vm = viewModel
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 lateinit property has not been initialized 错误发生 - lateinit property has not been initialized error occur 错误:lateinit 属性 recyclerViewAdapter 尚未初始化 - Error: lateinit property recyclerViewAdapter has not been initialized lateinit 属性 NotaViewModel 尚未初始化错误 - lateinit property NotaViewModel has not been initialized Error lateinit 属性尚未初始化 - lateinit property has not been initialized 在 onCreateView 中初始化的变量出现“Lateinit 属性尚未初始化”错误 - “Lateinit property has not been initialized” error on a variable initialized in onCreateView Hilt 和 WorkManager 错误:lateinit 属性 WorkerFactory 尚未初始化 - Hilt and WorkManager error : lateinit property WorkerFactory has not been initialized lateinit属性尚未初始化,我找不到初始化它的方法 - lateinit property has not been initialized I can not find a way to Initialize it 为什么我粉碎了“lateinit属性注入器尚未初始化”? - Why i crushed with “lateinit property injector has not been initialized”? 如何在 kotlin 中初始化 lateinit 属性。 错误“kotlin.UninitializedPropertyAccessException:lateinit 属性学生尚未初始化” - How to initialize lateinit property in kotlin. error "kotlin.UninitializedPropertyAccessException: lateinit property student has not been initialized" lateinit 属性 ABC 尚未初始化 - lateinit property ABC has not been initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM