DataBindingUtil.setContentView - Type parameter T has incompatible upper bounds
The binding activity is automatically generated and takes the name from the layout file, not the activity class.
So if you have an activity named BeautifulActivity
and the corresponding layout named sweet_layout.xml
, then the generated name will be SweetLayoutBinding
and not BeautifulActivityBinding
.
Don't make my same mistake by confusing between MainActivity
and activity_main.xml
ð
Source Android Developers
Try This Work for sure...
Step 1: Add this code in the build.gradle(Mobile:app)
dataBinding {
enabled = true
}
Example:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
**dataBinding {
enabled = true
}**
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
.....
}
Step 2:
Binding Can be done with Name of the .xml file as below example..
Simply name of the xml file and prefix with binding...
Example 1:
if of your .xml file is activity_main.xml then Binding file should be MainActivityBinding
Example 2:
if of your .xml file is android_sunil.xml then Binding file should be AndroidSunilBinding
Step 3: Sample Code:
public class BaseObservableActivity extends AppCompatActivity {
private ActivityBaseobservableBinding activityMainBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_baseobservable);
}
}
In the above code my .xml file name is activity_baseobservable so my binding class should be ActivityBaseobservableBinding activityMainBinding
--Happy Android Coding@Ambilpura
When I first meet this error, I create a layout named a.xml, and then I Create a Activity like this
public class ABinding extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ABinding binding = DataBindingUtil.setContentView(this, R.layout.a);
}
}
and this error occurs. Finally I found out Class ABinding was automatically generated in /build, so activity with name ABinding will overwrite the auto generated class
so I rename the Activity and the error disappear
I had the same problem. I tried a couple of things, Clean and Rebuild project.
But, It worked after I choose File -> Invalidate Caches / Restart