无法使双向数据绑定工作

时间:2017-04-16 08:26:56

标签: android data-binding

我想在按钮点击时将EditText s的值绑定到POJO,但POJO为空。以下是我写的代码: -

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类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;
    }
}

2 个答案:

答案 0 :(得分:1)

我认为您收到了NullPointerException,因为您没有设置用户值。

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

答案 1 :(得分:1)

2路数据绑定中的第一个不需要事件只需@ = {}将在视图上执行的每个事件上设置变量

第二个您在数据绑定中显示的变量需要设置else检查是否为null 由于在oncreate方法上没有调用binding.setuser,它将为null并且绑定将调用 User.name在文本视图中设置它,最终将导致空指针

同时查看绑定文档 他们已经确定了 @bindable变量 以及通知财产已更改