Reputation: 1697
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'
buildToolsVersion '23.0.0 rc2'
In the project build.gradle file, I have included this in my dependencies :
classpath "com.android.tools.build:gradle:1.3.0-beta2"
classpath "com.android.databinding:dataBinder:1.0-rc0"
The layout file is exactly the same as the one given in the guide.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.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>
Initially it gave an error Element layout does not have the required attribute layout_width and layout_height
.
I tried to fix it by assigning match_parent to both. Then I got the error
Error parsing XML: duplicate attribute
on the lines where i assigned layout_height
and layout_width
to the linear layout.
Again I tried to fix this by removing these attributes. Now every time I try to compile, I see this- error: package my.package.name.databinding
does not exist.
The code completion is working perfectly in my Fragment where I am trying to use this layout.
So what did I miss ?
Upvotes: 33
Views: 31710
Reputation: 5
// 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;
}
}
Upvotes: -1
Reputation: 1033
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'
Upvotes: 1
Reputation: 1854
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.
Upvotes: -1
Reputation: 7685
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'
}
Upvotes: 0
Reputation: 5964
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
Upvotes: 20
Reputation: 2267
In my case, the problem was that I was adding layout_height
and layout_width
to the layout
tag and also to my root layout. I've just removed both attributes from the layout
tag and solved the problem.
Upvotes: 0
Reputation: 3624
You have to be very careful that your POJO class data members should be public
if you are not creating the getter setters.
If you have simple POJO without any getter or setter
public class User {
public final String firstName;
public final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
If you make getter setter, javaBean style class
public class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Upvotes: 0
Reputation: 121
According to the current version of the guide (2015-11-11) you just have to add two dependencies: In the file build.gradle of the project add the gradle dependency. Currently the most recent version is 1.5.0-beta3
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0-beta3'
}
And in the file build.gradle of the module include the dataBinding section:
android{
...
dataBinding {
enabled = true
}
...
}
The error
error: package my.package.name.databinding does not exists
may ocurr because a mismatch between the xml and the POJO class
Upvotes: 0
Reputation: 9296
it’s usually because there is an error in your XML layout file and it can’t generate the binding object. Make sure you write data type correctly, check lower or upper of your writing.
<data>
<variable name="yourObject" type="com.example.simple.YourObject"/>
</data>
And make sure object is right calling:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{yourObject.name}"/>
<TextView
Upvotes: 3
Reputation: 304
removing apply plugin: 'com.neenbedankt.android-apt'
from my build.gradle solved my problem.
Upvotes: 17
Reputation: 3645
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.
Upvotes: 1
Reputation: 38263
Looks like you forgot to add data binding plugin to your app's gradle file:
apply plugin: 'com.android.databinding'
You have to apply it to any module you want to use data binding.
Also, you should not add layout_w/h to the layout
tag, it is just an Android Studio bug, already fixed in aosp.
Upvotes: -1