android - 找不到符号 CountryFragmentBindingImpl
问题描述
无论更改是什么,我在构建项目时都无法摆脱这个错误。有人可以帮我解决这个问题吗?
我还在 app.gradle 中添加了 databinding enabled = true ,但这并没有解决我的问题。
这是我的 countries_fragment.xml
<?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">
<data>
<import type="android.view.View" />
<variable
name="viewmodel"
type="com.example.android.covidupdates.CountriesViewModel" />
</data>
<RelativeLayout
android:id="@+id/tasks_container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/tasks_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="@{viewmodel.dataLoading ? View.GONE : View.VISIBLE}">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tasks_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:items="@{viewmodel.countries}" />
</LinearLayout>
</RelativeLayout>
</layout>
这里是 CountryFragment.kt
class CountriesFragment : Fragment() {
private val viewModel by viewModels<CountriesViewModel> { ViewModelFactory(this) }
private lateinit var viewBinding : CountriesFragmentBinding
companion object {
fun newInstance() = CountriesFragment()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewBinding = CountriesFragmentBinding.inflate(inflater, container, false).apply {
viewmodel = viewModel
}
return viewBinding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewBinding.lifecycleOwner = this.viewLifecycleOwner
}
}
和我的 ViewModelFactory.kt
class ViewModelFactory constructor(
owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
override fun <T : ViewModel> create(
key: String,
modelClass: Class<T>,
handle: SavedStateHandle
) = with(modelClass) {
when {
isAssignableFrom(CountriesViewModel::class.java) ->
CountriesViewModel()
else ->
throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.name}")
}
} as T
}
解决方案
我猜您的问题出在这一行的 xml 中:
app:items="@{viewmodel.countries}"
当你在xml中出现问题时,Data Binding 可以在构建过程中生成类 CountryFragmentBindingImpl 的过程中给出错误。
也许这个讨论会有所帮助。我可以推荐的 - 是在适配器的帮助下实现在 RecyclerView 中设置项目的常用方法
推荐阅读
- javascript - 读取 npm 包的文件
- regex - 用于简单自定义匹配的正则表达式
- node.js - 如何在一个数组中发送多个 Formdata 以表达后端
- html - 底部的 Div - 再次不想在角度工作
- c++ - 复制 std::vector
使用 memcpy 使 * 缓冲区无效 - angular - 无法解析服务提供商的所有参数?
- git - 经常应用和删除提交
- angular - 角度通用 SSR 构建流程如何工作以及如何自定义 web-pack 服务器配置,例如在编译时减少 CSS 类名?
- c++ - 通过 C for 循环中的子例程递增
- python - 如何在树莓派中运行这个程序