找不到符号CountrysFragmentBindingImpl

问题描述 投票:0回答:2

无论进行什么更改,在构建项目时我都无法摆脱此错误。有人可以帮我解决这个问题吗?

我还在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>

这是CountrysFragment.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

}

android android-fragments android-databinding android-viewmodel
2个回答
1
投票

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

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

当您在xml中遇到问题时,数据绑定会在构建期间生成类CountrysFragmentBindingImpl的过程中给出错误。

也许this discussion会有所帮助。我可以推荐的-是借助Adapter

在RecyclerView中实现设置项目的常规方法

0
投票

您是否在应用程序级别的build.gradle文件中添加了apply plugin: 'kotlin-kapt'。您也可以在here处签出样本以供参考。

© www.soinside.com 2019 - 2024. All rights reserved.