I am trying to test data binding as given in the guide here. I have included this in my build.gradle file (of module app) :
compileSdkVersion \'android-MNC\
Your bindings in the xml file might be invalid.
Make sure to double check
type
attributes valid reference to data object? type="my.package.Class"
name="client"
-> "@{client.field}"
public
or encapsulated with gettersOldNameBinding -> NewNameBinding
You may have a problem or mismatch between your model and your layout file. In my case I had:
...
<TextView
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/photo"
android:layout_toStartOf="@+id/photo"
tools:text="0912454433"
android:textStyle="bold"
android:text="@{contact.cellPhoneNumber}"
android:layout_below="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
but my model class was like so:
public class MyContact {
public String name;
public String cellphoneNumber; // <-- Notice the lowercase 'p'
}
I also suffer with the same problem. But my mistake is
User class variables declared as not a public. So we should declare the POJO class variables as public.
public class User {
public String firstName,lastName;
public User(String fname,String lname){
this.firstName=fname;
this.lastName=lname;
}
}
Then solved my problem.
This appears to be an issue in android studio. The editor will flag it as an error however it will still compile.
I had the same issue and so I cloned this example repo to test my system is configured correctly to support the databinding api https://github.com/mgrzechocinski/android-bindings-example
Ignore the error warning on the layout
tag. It is wrong.
Something I noticed is you don't necessarily require the data
tag if are simply using the databinding to get access to the views items through the BindingActivity class that is generated.
If you are using 'android-apt' plugin for dagger or other libraries, after enabling data binding, you have to remove 'android-apt' plugin and use 'provided' instead of 'apt' in your dependencies. For example if you are using dagger, remove this
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
and this
apply plugin: 'com.neenbedankt.android-apt'
and then replace
apt 'com.google.dagger:dagger-compiler:2.0'
with
provided 'com.google.dagger:dagger-compiler:2.0'
// This is under activity_main xml .
<data>
<variable
name="student"
type="com.example.vishalkamboj.testdatabinding.Student" />
</data>
// This is Student Class
public class Student {
public String firstname ; public String lastname ;
public Student(String firstname , String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
}