Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

I cant solve this error "lateinit property dataBinding has not been initialized" in android ?

By M.K. Verma · 4 May 2022

I cant solve this error "lateinit property dataBinding has not been initialized" in android ?

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.

So you can do this options to fix the issue:

  1. call super method with 2 params in your HomeActivity

    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

    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)
           ...