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
}