Can't resolve Android databinding class

79,506

Solution 1

Thanks to all for your answer.I found solution with ContentMainBinding class name for data binding. Lets me explain.

NOTE: While using layout with <include ... here is <include layout="@layout/content_main" having Data Binding functionality, the class name related to include layout name. Here is the ContentMainBinding

My layout file are as below:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.databindingdemo.app.MainActivity">
    ...
    <include layout="@layout/content_main" />
    ...
    </android.support.design.widget.CoordinatorLayout>

And content_main.xml is layout where I added my Data Binding layout code.

So instead of using MainActivityBinding it can resolved with ContentMainBinding

The code which work for me is below:

//Code for data binding
    ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
    user = new User("Pranay", "Patel", "[email protected]", "9999999999");
    contentMainBinding.setUser(user);

DONE.

Solution 2

DataBinding class will be generated based on your xml file name. It is clearly mentioned in doc you are following.

By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding

If your xml name is activity_main.xml than DataBinding class name will be ActivityMainBinding.

If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

Dont forget to clean and build project once

you can follow this tutorial for more about DataBinding

Solution 3

TRY Renaming the xml file to another name and check if binding works once it works rename it back to the one that was used.

That helped for Android Studio 3.1

Solution 4

Make sure your activity_main.xml file is enclosed with layout tags like so:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.constraint.ConstraintLayout>
</layout>

Solution 5

If DataBinding class name is correct and you used to clean and rebuild project but it still show error.
Then you try to restart AndroidStudio

Share:
79,506
pRaNaY
Author by

pRaNaY

Software Engineer | #AndroidDev | Kotlin❤️ | Flutter 💙| Public Speaker 🗣️| MobileSecurity📱 | Cooking 👨‍🍳| Open Source Enthusiastic https://pranaypatel512.github.io/ http://booleancoder.com/ He believes to learn something new every day! | Someday, I'll be a great coder. Try it out, improve it, practice more and have fun! ✌️

Updated on February 20, 2022

Comments