Categories
android android-databinding

Android two way binding with Integer type causes databinding does not exist

I’m having some issue with implementing two way binding with an Integer data type.

public class User {
private String firstName;
private String lastName;
private int age;
public User() {}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data class="UserDataBinding">
<variable
name="user"
type="com.databinding.model.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.firstName}" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.lastName}" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.age}" />
</LinearLayout>
</layout>

Unfortunately, it gives me the error

“Error:(52, 17) Cannot find the getter for attribute ‘android:text’
with value type java.lang.Integer on
android.support.design.widget.TextInputEditText. “

If I change the attribute text to

       <EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={Integer.toString(user.age)}" />

then I get the error

“Error:cannot generate view binders java.lang.NullPointerException”

Appreciate any help on this.

UPDATE: It seems there was another error right after the error mentioned above.

cannot generate view binders java.lang.NullPointerException

Not sure why its giving me NPE even though the app hasn’t started yet.