Home > OS >  I cant solve this error "lateinit property dataBinding has not been initialized"
I cant solve this error "lateinit property dataBinding has not been initialized"

Time:04-30

I have base activity< 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"

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

CodePudding user response:

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

CodePudding user response:

Because in your base class the onCreate() with 2 parameters is not going to get called during the activity's lifecycles. And in your subclass you override the onCreate() with a parameter.

Just simple change your base class to override the onCreate() with a parameter to fix the problem. And the other thing is you implement these class the java's way.

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
    }
}
  • Related