Data Binding Android - Type parameter T has incompatible upper bounds : ViewDataBinding and MainActivity

AndroidData BindingAndroid Databinding

Android Problem Overview


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
}

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

You should use:

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

In onCreate

MainActivity isn't the generated Binding class.

Solution 5 - Android

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.

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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"

Solution 9 - Android

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

Solution 10 - Android

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...

Solution 11 - Android

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.

Solution 12 - Android

Do this

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

Instead of

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

Solution 13 - Android

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)

Solution 14 - Android

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.

Solution 15 - Android

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

Solution 16 - Android

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

Solution 17 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChirag ShenoyView Question on Stackoverflow
Solution 1 - AndroidFelipe GualbertoView Answer on Stackoverflow
Solution 2 - AndroidAndrew KellyView Answer on Stackoverflow
Solution 3 - AndroidWeiqi.XieView Answer on Stackoverflow
Solution 4 - AndroidGeorge MountView Answer on Stackoverflow
Solution 5 - AndroidPratik ButaniView Answer on Stackoverflow
Solution 6 - AndroidAnkitView Answer on Stackoverflow
Solution 7 - AndroidLibin ThomasView Answer on Stackoverflow
Solution 8 - AndroidAnil Chandra Varma DanduView Answer on Stackoverflow
Solution 9 - AndroidRushi AyyappaView Answer on Stackoverflow
Solution 10 - AndroidKetan RamaniView Answer on Stackoverflow
Solution 11 - AndroidAbdulsalam OpeyemiView Answer on Stackoverflow
Solution 12 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 13 - AndroidUzairView Answer on Stackoverflow
Solution 14 - AndroidAjay ChauhanView Answer on Stackoverflow
Solution 15 - AndroidAlexPadView Answer on Stackoverflow
Solution 16 - AndroidAshwin NirmaleView Answer on Stackoverflow
Solution 17 - Androidblitzkerig_brogrammerView Answer on Stackoverflow