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

14,987

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
14,987
BrianM
Author by

BrianM

Updated on July 01, 2022

Comments

  • BrianM
    BrianM almost 2 years

    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