Reputation: 577
I have a Custom Linear layout and an xml file in with 3 checkboxes. The Custom linear layout looks roughly like this:
class AdvancedBox : LinearLayout, DefineExchangesDialog.DefineExchangesDialogListener {
private lateinit var mBinding: AdvancedBoxBinding
private lateinit var viewModel: GlobalConfigViewModel
constructor(c: Context) : super(c) {
initLayout()
}
constructor(c: Context, a: AttributeSet) : super(c, a) {
initLayout()
}
private fun initLayout() {
val inflater = LayoutInflater.from(context)
mBinding = AdvancedBoxBinding.inflate(inflater)
}
override fun getRootView(): View {
return mBinding.root
}
fun setViewModel(viewModel: GlobalConfigViewModel){
mBinding.viewModel = viewModel
}
override fun onFinishInflate() {
super.onFinishInflate()
//cbVolumeChange is an id defined in advanced_box.xml
cbVolumeChange.setOnClickListener(this::onShowAdvanced)
cbExchanges.setOnClickListener(this::onShowAdvanced)
cbPriceFilter.setOnClickListener(this::onShowAdvanced)
}
The problem is that setting an onClickListener on any of the checkboxes in onFinishInflate() will cause a NullPointerException. I thought overriding the getRootView() would be the solution but that did not work. Something that would work is the following
val root = mBinding.root
root.cbVolumeChange ...
But that is not something I would like to do. So what would be the right way to use the Android Kotlin extension with databinding in my case?
Upvotes: 0
Views: 783
Reputation: 39843
The issue is, that the inflated view is not attached to the layout. You should use AdvancedBoxBinding.inflate(inflater, this, true)
for it.
After that onFinishInflate()
doesn't matter for you, as it's only relevant for inflating the view and the hierarchy from XML not from code as you're doing.
Upvotes: 1