首页 > 解决方案 > 找不到符号 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

}

标签: androidandroid-fragmentsandroid-databindingandroid-viewmodel

解决方案


我猜您的问题出在这一行的 xml 中:

app:items="@{viewmodel.countries}"

当你在xml中出现问题时,Data Binding 可以在构建过程中生成类 CountryFragmentBindingImpl 的过程中给出错误。

也许这个讨论会有所帮助。我可以推荐的 - 是在适配器的帮助下实现在 RecyclerView 中设置项目的常用方法


推荐阅读