Android: 找不到符号 class DataBinderMapperImpl。找不到 android:state_checked 的 getter

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

我正在尝试将双向数据绑定与匕首柄和视图模型结合使用。但是,我的问题是我的构建失败,出现错误...

错误代码

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

看起来 Android 无法创建我的数据绑定的 impl 文件 xml。

这是我的代码:

XML 文件,包含在我的 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在这里初始化

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

电子邮件片段

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,问题出现在这里!

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

视图模型

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>()
}

希望代码不要太多或乱七八糟。我试图删除对我的问题不重要的所有内容(例如布局内容或不必要的功能)。我的 build.gradle.

中启用了数据绑定

知道这个问题已经有人问过,但没有人使用匕首柄,我认为这是我的问题吗?感谢您的帮助,谢谢!

编辑

我更改了我的 BaseFragment class 和 CalibrateMessageFragment class 以附加我的视图模型。不幸的是,这并没有解决我的问题...

编辑 2

好的,我更进一步发现,com.google.android.material.card.MaterialCardView 没有为 android:state_checked 提供 getter?我的错误至少说明了以下内容:

[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}]}

但这没有任何意义,因为 here 您可以看到 CardView 为 isChecked..

提供了一个 getter

好的,我已经通过编写自己的 BindingAdapter.kt 解决了问题,这是代码:

@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()
}

唯一的问题是我无法实现一种逻辑,使我的 MaterialCardView 像一个 Radio 组一样工作。伤心