I'm trying to move to kotlin in android, and tried data-binding like this:
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_authentication)
viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
.create<AuthViewModel>(AuthViewModel::class.java)
//mBinding.auth.invoke(viewModel)
mBinding.auth.invoke(viewModel)
mBinding.setLifecycleOwner(this)
It gave me can't be invoked as function, so auto-generated this:
public void invoke(@NotNull AuthViewModel viewModel) {
}
...
But it keeps saying it's mutable, can be changed by now.
How should I resolve this?
And gradle file says, I should use kapt, I don't know what is that
try initilalizing binding before oncreate by
private lateinit var binding : /*your layout name in camelcase*/Binding /*like ActivityMainBinding*/
private lateinit var viewmodel : AuthViewModel
then in onCreate
binding = ActivityMainBinding.inflate(layoutInflator)
viewmodel = ViewModelProvider(this,factory).get(AuthViewModel::class.java)
also make sure you addedbuildFeatures { dataBinding true }
in app gradle file`inside android
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.