Search code examples
androidkotlinandroid-databindingdaggerdagger-hilt

Android: Cannot find symbol class DataBinderMapperImpl. Cannot find a getter for android:state_checked


I am trying to use two-way data binding in combination with dagger hilt and viewmodel. However, my problem is that my build fails, with the error...

Error Code

Task :app:kaptDebugKotlin
C:\Users\Censored\AndroidStudioProjects\Example\app\build\generated\source\kapt\debug\com\example\app\DataBinderMapperImpl.java:9: error: cannot find symbol
import com.example.app.databinding.CalibrateRepairMessageContentBindingImpl;
                                  ^
  symbol:   class CalibrateRepairMessageContentBindingImpl

Task :app:kaptDebugKotlin FAILED
  location: package com.example.app.databinding

It looks like Android is not able to create the impl File of my databinding xml.

Here is my code:

XML File, included in my fragment_calibrate_repair_message

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewModel"
            type="com.example.app.data.viewmodel.EmailViewModel" />
    </data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/calibrate_repair_message_input">

        <com.google.android.material.textfield.TextInputEditText
            <!-- DATA BINDING -->
            android:text="@={viewModel.etMessage}"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/calibrate_repair_ex_option_ONE"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        <!-- DATA BINDING -->
        android:state_checked="@={viewModel.cardOptionOneChecked}">

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/calibrate_repair_ex_option_TWO"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        <!-- DATA BINDING -->
        android:state_checked="@={viewModel.cardOptionTwoChecked}">

    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

BaseFragment, dataBinding initialized here

abstract class BaseFragment<B: ViewDataBinding>(val layout: Int) : Fragment() {
    //... Some variables that have nothing to do with dataBinding

    // Here I am using Databinding! Using BaseFragment because all my fragments use dataBinding
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = DataBindingUtil.inflate<B>(inflater, layout, container, false)
        return binding.root
    }
}

Email Fragment

abstract class EmailFragment<B: ViewDataBinding>(
    layout: Int,
    //... etc
) : BaseFragment<B>(layout) {
    //... Some variables that have nothing to do with dataBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //... Some functions that have nothing to do with dataBinding
    }

CalibrateRepairMessageFragment, Problem occures here!

class CalibrateRepairMessageFragment(//...): 
// Here I am providing the actual DataBiding class "CalibrateRepairMessageContentBinding"
EmailFragment<CalibrateRepairMessageContentBinding>(
        R.layout.fragment_calibrate_repair_message,
       //..
    ) {

ViewModel

class EmailViewModel @ViewModelInject constructor(
    @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {

    // My three properties which are declared in my layout.xml
    val etMessage = MutableLiveData<String>()

    val cardOptionOneChecked = MutableLiveData<Boolean>()
    
    val cardOptionTwoChecked = MutableLiveData<Boolean>()
}

I hope the code is not too much or messy. I tried to delete everything that is not important for my question (such as layout stuff or unnecessary functions). Databinding is enabled in my build.gradle.

I KNOW that this question was already asked but no one of them used dagger-hilt and I think that is my problem here? I appreciate every help, thank you!

EDIT

I've changed my BaseFragment class and CalibrateMessageFragment class to attach my viewmodel. Unfortunately that did not solve my problem...

  • New BaseFragment

     abstract class BaseFragment<T: ViewDataBinding>(val layout: Int) : Fragment() {
     abstract val viewModel: ViewModel // new
    
     // New
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         return DataBindingUtil.inflate<T>(inflater, layout, container, false).apply {
             lifecycleOwner = viewLifecycleOwner
             setVariable(BR.viewModel, viewModel)
         }.root
     }
    }
    
  • CalibrateMessageFragment

     class CalibrateRepairMessageFragment(//...): 
         EmailFragment<CalibrateRepairMessageBinding>(
                 R.layout.fragment_calibrate_repair_message,
                //..
             ) {
           // New
          override val viewModel: EmailViewModel by viewModels()
     }
    

EDIT 2

Okay, I've got a step further and found out, that com.google.android.material.card.MaterialCardView does not provide a getter for the android:state_checked? My error says at least the following:

[databinding] {"msg":"Cannot find a getter for \u003ccom.google.android.material.card.MaterialCardView android:state_checked\u003e that accepts parameter type \u0027java.lang.Boolean\u0027\n\n
If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.","file":"app\\src\\main\\res\\layout\\calibrate_message_content.xml","pos":[{"line0":63,"col0":8,"line1":135,"col1":59}]}

But this doesn't make any sense because here you can see that the CardView provides a getter for isChecked..


Solution

  • Ok, I've solved the problem by writing my own BindingAdapter.kt, here is the code:

    @BindingAdapter("state_checked")
    fun setStateChecked(view: MaterialCardView, liveData: MutableLiveData<Boolean>) {
        if (view.isChecked != liveData.value) {
            liveData.value = view.isChecked
        }
    }
    
    @InverseBindingAdapter(attribute = "state_checked")
    fun getStateChecked(view: MaterialCardView,): Boolean {
        return view.isChecked
    }
    
    @BindingAdapter("state_checkedAttrChanged")
    fun setCheckedAttrListener(
        view: MaterialCardView,
        attrChange: InverseBindingListener,
    ) {
       view.setOnClicklistener {
       // logic is here
       }
       attrChange.onChange()
    }
    

    The only problem is that I can't figure out to implement a logic so that my MaterialCardView acts like a Radio group. Sad.