OnClick Databinding无法在android中运行

时间:2017-04-18 13:49:22

标签: android data-binding

我有这个班级用户

Class User

我试图在android中实现onClick绑定我也有这个Presenter类

Presenter Class

我正在初始化我的绑定 enter image description here

同样在我的XML文件中

enter image description here

我设置了我的变量但是在编译时遇到了这个错误 enter image description here

4 个答案:

答案 0 :(得分:3)

您正在传递View参数,而Presenter类中的方法未收到该参数。 要解决此问题,请将您的方法更改为:

 public class Presenter{
        public void onSaveClick(View view, String task){
            Log.i("log", "Action" + task)
        }

然后,将它传递给你的xml:

android:onClick="@{(v) -> presenter.onSaveClick(v, user.address)}"

答案 1 :(得分:0)

更改xml

android:onClick="@{() -> presenter.onSaveClick(user.address.get ())}"

或演示者

onSaveClick (ObservableField<String> address){

Log.d ("log", address.get ())
}

答案 2 :(得分:0)

用户模型:

private ObservableField<String> address = new ObservableField<>();

public String getAddress() {
    return address.get();
}

public void setAddress(String address) {
    this.address.set(address);
}

Xml:

<Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.address}"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       android:layout_marginBottom="8dp"
       android:onClick="@{(v) -> event.onSaveCLick(user.address)}"/>

爪哇:

public class Presenter{
        public void onSaveCLick(String task){
            Toast.makeText(MainActivity.this,"Address"+task,Toast.LENGTH_SHORT).show();
        }
    }

答案 3 :(得分:0)

因为您错误地引用了演示者!。错误是在XML中设置变量时输入了错误的包。它实际上与android:onClick无关,但事实上你引用了演示者,而没有绑定它。在设置演示者后的活动中,您必须执行mainBinding.setVariable(BR.presenter,presenter);那之后它应该工作得很好。