Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding: cannot find ...BindingImpl in generated databinding file

I am trying to databind a viewmodel using the example project android-sunflower. The current issue is that when I am trying to build the project I get the error error: cannot find symbol symbol: class FragmentShopBindingImpl location: package {{packageName}}.databinding in the class DataBindinMapperImpl I'm not really sure what I am missing here, since I added everything from the example project. The class FragmentShopBindingImpl does not get generated, or shouldn't it? Since I cannot see any occurence of a class ending with 'Impl' in the android sunflower example
My code:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val factory = InjectorUtils.provideShopViewModelFactory(context!!)
        val shopViewModel = ViewModelProviders.of(this, factory)
            .get(ShopViewModel::class.java)

        val binding = DataBindingUtil.inflate<FragmentShopBinding>(
            inflater, R.layout.fragment_shop, container, false).apply {
            viewModel = shopViewModel
            lifecycleOwner = this@ShopFragment
        }

        return binding.root
    }

Layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewModel"
            type="{{packageName}}.viewmodel.ShopViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".fragments.ShopFragment">

        <TextView
            android:text="@{viewModel}"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</layout>

Image of generated file (ignore the {{packageName}}:

enter image description here

like image 342
BrianM Avatar asked Mar 31 '19 13:03

BrianM


3 Answers

It seems the only thing I had to add was <import type="android.view.View" /> in the data tags...

like image 112
BrianM Avatar answered Oct 09 '22 13:10

BrianM


In your xml code inside textView tag, for android:text attribute you have used @{viewmodel}. It just refers your shopViewModel class, you must target the text variable inside that class. Then the gen. class file errors will vanish.

bindingImpl errors are mostly generated for invalid assignment for XML-text or XML-onClick attributes.

like image 41
Willey Hute Avatar answered Oct 09 '22 11:10

Willey Hute


If you use two-ways databinding (@={myBindingValue}, with the '=' sign instead of @{myBindingValue}) sometimes, you'll have this unusefull generic error because the value you are trying to bind is declared as immutable => val instead of var in Kotlin in your data class.

Exemple :

data class User(
   val name,
   var email
)

In this example, you could bind the user's email variable as : text="@={myViewModel.user.email}" But, if you try to bind the user's name : text="@={myViewModel.user.name}" you will get this error.

like image 22
A.Mamode Avatar answered Oct 09 '22 13:10

A.Mamode