1

Whatever the changes are, I was unable to get rid of this error while building the project. Can someone help me resolving this issue ?

I also added databinding enabled = true in app.gradle which didn't resolve my issue.

Here is my 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>

and here is the CountriesFragment.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
}

}

and my 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

}

Bharath
  • 391
  • 2
  • 9

1 Answers1

2

My guess that your problem is in your xml in this row:

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

When you have problems in xml, then Data Binding can give the error in the process of generating class CountriesFragmentBindingImpl during build.

Maybe this discussion will be helpful. What I can recommend - is to implement usual way of setting items in RecyclerView with the help of Adapter

sergiy tikhonov
  • 4,961
  • 1
  • 10
  • 27
  • Thanks for the suggestions. What you said was right. I had to add the Binding Adapter to use app:items in the recycler view. – Bharath Apr 14 '20 at 03:50