Parag Kadam
Parag Kadam

Reputation: 3850

Unable to get 2-way data binding to work

I want to bind the value of EditTexts to a POJO on button click but POJO is null. Following is the code I have written:-

MainActivity:-

public class MainActivity extends AppCompatActivity {


    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button)
    public void submit()
    {
        Log.d("myapplication",binding.getUser()); // NullPointerException
    }
}

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="user"
            type="com.paragkadam.databindingsample.User"/>
    </data>


    <RelativeLayout

        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.paragkadam.databindingsample.MainActivity">

    <EditText
        android:id="@+id/first_name_edittext"
        android:layout_margin="10dp"
        android:hint="First name"
        android:text="@={user.firstName}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/last_name_edittext"
        android:layout_margin="10dp"
        android:hint="Last name"
        android:text="@={user.lastName}"
        android:layout_below="@+id/first_name_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/button"
        android:layout_margin="10dp"
        android:layout_below="@+id/last_name_edittext"
        android:text="Submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </RelativeLayout>

</layout>

POJO class User.java :-

public class User {
    private String firstName,lastName;

    public User(){}
    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

Upvotes: 0

Views: 149

Answers (2)

Parth Dave
Parth Dave

Reputation: 2126

First of 2 way databinding does not require event just @={} will set variables on every event that performed on view

Second every variable that you write to show in data binding need to set else check for nullchecks As binding.setuser not called on oncreate method it will be null and binding will call User.name to set it in text view which will ultimately lead to null pointer

Also check out binding docs They have set @bindable to variables As well as notified property changed

Upvotes: 1

S Haque
S Haque

Reputation: 7271

I think you are getting NullPointerException because you did not set the user value.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        binding.setUser(new User("FirstName","LastName"));
    }

Upvotes: 1

Related Questions