BrianM
BrianM

Reputation: 949

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

Upvotes: 11

Views: 19703

Answers (8)

K. H. Tam
K. H. Tam

Reputation: 41

I had a similar error message but the problem is different. Although I see that OP's problem had been solved, the same error message may often pop up when playing with the MVVM architecture. (I'm giving my answer in Java btw, but the problems can be solved in the similar manner)

Basically, when this error message

error: cannot find symbol symbol:   class FragmentShopBindingImpl
  location: package {{packageName}}.databinding

pop up, it doesn't tell you anything about what's wrong. It pops up when the compiler failed to generate the xxxBindingImpl class (which should have been auto-generated) due to some issues with your binding.

For my case, inspecting the actual source of error by running ./gradlew :app:build --stacktrace helped a lot, and mostly it's one of the following problems:

  1. Name crashes in your xml (particularly, when you have multiple views with the same id that require the same binding class.)
  2. Some attributes don't have default binding adapters, e.g. android:layout_width and android:src. In this case you'll need to set up the binding adapter for the attribute yourself by adding @BindingAdapter tag:
    @BindingAdapter("android:src") //<-----
    public static void setIcon(ImageView view, int drawable){ view.setImageResource(drawable); }
  1. Missing imports: When you're using classes that are not one of the primitive types in the xml, you must import the class with FULL PATH as follows:
    <data class="BoxBinding">
        <import type="android.view.View"/>  //<-----
        <variable
            name="boxData"
            type="{{packageName}}.views.boxlists.BoxList.BoxData" />
    </data>

Upvotes: 0

Akash Jain
Akash Jain

Reputation: 730

The issue for me was , there were multiple qualifier layout's for example I had one for Night Mode and the base file, I was only making changes in the base file but not in other qualifier resource file, once I made the changes in all qualifiers, it worked for me.

Upvotes: 0

Rohit
Rohit

Reputation: 516

I had a BindingAdapter tag that was present in an unreachable module.

Upvotes: 0

Rubber Band Republic
Rubber Band Republic

Reputation: 16

I had the same error show up but it was because my android:text="@={...}" was outside the editText.

Upvotes: 0

faribakhandani
faribakhandani

Reputation: 49

You have to define variable as ObservableField below :

 public final ObservableField<String> name = new ObservableField<>();
 public final ObservableField<String> family = new ObservableField<>();

Upvotes: 0

A.Mamode
A.Mamode

Reputation: 1577

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.

Upvotes: 4

Willey Hute
Willey Hute

Reputation: 1078

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.

Upvotes: 8

BrianM
BrianM

Reputation: 949

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

Upvotes: 10

Related Questions