56

I am using Android Studio 2.0 Preview 4. I'm using Android SDK tools 25 rc1. This error persists no matter how many times I clean / rebuild project. File->Invalidate Caches and restart also doesn't work. I'm not able to run the most basic example of data binding.

build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.chiragshenoy.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

MainActivity.java

package com.example.chiragshenoy.myapplication;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivity binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        User user = new User("Test", "User");
        binding.setUser(user);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="user" type="com.example.chiragshenoy.myapplication.User"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.firstName}"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.lastName}"/>
    </LinearLayout>
</layout>

this is my top level build.gradle file

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Chirag Shenoy
  • 613
  • 1
  • 5
  • 10

17 Answers17

235

If your gradle version is ok (1.5+) then you should try this:

  1. Go to your "some_layout.xml"
  2. Click right -> Refactor -> Rename (or SHIFT + F6)
  3. Rename your layout to "some_layout2.xml" for example
  4. Rename this file back to the original "some_layout.xml"

This might fix the problem. Let us know.

Martin Gottweis
  • 2,721
  • 13
  • 27
Felipe Gualberto
  • 2,389
  • 1
  • 11
  • 4
41

This solution worked for me "File -> Invalidate Caches / Restart" https://stackoverflow.com/a/32191257/2205809

Community
  • 1
  • 1
Andrew Kelly
  • 2,180
  • 2
  • 19
  • 27
6

There is a simple solution.

  1. click "File" on toolbar of android studio
  2. click "Invalidate Caches and Restart", then it works.

Hope this answer will help you.

Weiqi.Xie
  • 61
  • 1
  • 3
  • Welcome to SO! When you post an answer try to explain it a little bit, furthermore in your case, you are giving an answer again an image not attached. Even if it is right. – David García Bodego Oct 21 '19 at 07:22
6

I fixed this error by surrounding my xml layout file with the <layout> tag

your_layout.xml file should look something like

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

<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/view_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</layout>

This worked for me.

5

You should use:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

In onCreate

MainActivity isn't the generated Binding class.

George Mount
  • 20,708
  • 2
  • 73
  • 61
4

I just thought based on Felipe Gualberto's Answer and I got solution difference way.

If you have written multiple views with the same id in different XML then make it unique.

Thank you.

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
3

One of the quickest hack i use is rename the xml file back and to.

e.g.

rename abc_layout.xml to abc_layout2.xml. once the binding import shows error rename it back to abc_layout.xml.

the short cut to rename is shift + F6. its way quicker that entire project rebuild. this will only remove error from one file though.

Ankit
  • 429
  • 3
  • 16
3

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. The problem is you are trying to create binding class manually.

This class holds all the bindings from the layout properties (e.g. the user variable) to the layout's Views and knows how to assign values for the binding expressions. The easiest means for creating the bindings is to do it while inflating. No need to restart the Android Studio.

paulina_glab
  • 2,467
  • 2
  • 16
  • 25
Libin Thomas
  • 1,132
  • 13
  • 17
2

in top level Build.Gradle file use gradle 1.5.0 or above

 dependencies {
    classpath 'com.android.tools.build:gradle:1.5.0'
}

if it is lower than 1.5.0 then use this one also in top level gradle file

        classpath "com.android.databinding:dataBinder:1.0-rc4"
2

Just try to Invalidate caches/Restart from File. This worked for me.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32
2

I'm also getting the same problem after Migrating my existing project to AndroidX and i solve it by doing below things

Build -> Clean Project
Build -> Rebuild Project

After that

File -> Invalidate Caches/Restart...

Ketan Ramani
  • 4,874
  • 37
  • 42
1

Do this

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

Instead of

MainActivity binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

Check the name of the binding class, here ActivityMainInitBinding is generated based on the xml file activity_mail_init.xml. If you use wrong class the error pops up.

  val binding : ActivityMainInitBinding=
        DataBindingUtil.setContentView(this, R.layout.activity_main_init)
Uzair
  • 1,529
  • 14
  • 17
0

After migrating to AndoridX, You need to remove old v4 or v7 widgets from the XMl .For ex - you need to replace if you are using some AppCompat widget like

<android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="11sp"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:tag="showMore"/>

Replace it with andoridx widget

  <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/txt_summary_desc_more"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="11sp"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:text="Read More"/>

Worked well for me.

Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37
0

The refresh is of no use if it continues to give errors. When there are these errors it is likely that in your xml file there is a problem that the compiler did not show you when creating the layout. In fact, it may happen that this error is visible only in its "debug" folder. If an error of this type should occur, just follow the error in the debug folder and correct it from the original file. In my case the error was two duplicates of

 

xmlns: app = "http://schemas.android.com/apk/res-auto"

that I couldn't see

AlexPad
  • 10,364
  • 3
  • 38
  • 48
0

Delete module level build folder and re-open your project.

Ashwin Nirmale
  • 443
  • 4
  • 13
0

I was getting this error because i missed the tag in view xml, which is not the case here.