我正在尝试将双向数据绑定与匕首柄和视图模型结合使用。但是,我的问题是我的构建失败,错误...
错误代码
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 无法创建我的数据绑定 xml 的 impl 文件。
这是我的代码:
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 类和 CalibrateMessageFragment 类以附加我的视图模型。不幸的是,这并没有解决我的问题......
新的 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 } }
校准消息片段
class CalibrateRepairMessageFragment(//...): EmailFragment<CalibrateRepairMessageBinding>( R.layout.fragment_calibrate_repair_message, //.. ) { // New override val viewModel: EmailViewModel by viewModels() }
编辑 2
好的,我更进一步发现,这com.google.android.material.card.MaterialCardView
并没有为android:state_checked
? 我的错误至少说明了以下内容:
[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}]}
但这没有任何意义,因为在这里您可以看到 CardView 为 isChecked 提供了一个 getter..