Android数据绑定:对两个数据绑定使用@={}语法导致绑定文件不生成

Android Data Binding : Using the @={} syntax for two data binding causes the Binding file not to be generated

我正在尝试使用 android 2 路数据绑定库的 2 路数据绑定功能。

我的 gradle 版本是

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.1'
} 

当我使用库进行简单绑定时,它起作用了

 <EditText
            android:id="@+id/text_view_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:hint="Enter Username"
            android:inputType="text"
            android:text="@{model.username}"
        />

当我使用 @={} 格式进行 2 种数据绑定时,它不起作用

 <EditText
            android:id="@+id/text_view_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:hint="Enter Username"
            android:inputType="text"
            android:text="@={model.username}"
        />

没用。我收到以下错误

Error:(9, 31) error: package databinding does not exist

基本上数据绑定 classes 没有生成。请帮助如果需要任何其他信息,请告诉我。

public class Login implements IBaseModel {
    public String username, password;

    public Login(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }
}

并且视图模型 class 是

public class LoginViewModel implements IViewModel {
    private Subscription subscription;
    private ILoginDataListener dataListener;
    private Login login;

    Context context;


    String getUserName() {
        return this.login.getUsername();
    }

    LoginViewModel(Context ctx, Login login) {
        this.context = ctx;
        this.login = login;
    }

    public void onClickSubmit(View view) {
        Toast.makeText(context, "From View Model username is " + this.login.getUsername() + " the password " + this.login.getPassword(), Toast.LENGTH_SHORT).show();

        Toast.makeText(context, "From View Model", Toast.LENGTH_SHORT).show();

        loginRequest("Rajendra", "12345");
    }

    public void updateValue(View view) {
        this.login.password = "dummy";
        this.login.username = "simplefool";
    }

    @Override
    public void destroy() {

        if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe();
        subscription = null;
        context = null;
        dataListener = null;    
    }
}

来自我的评论:

There should be more information in the log cat, not as pretty as normally, but there is more. Please post it, and format it. But I do think that the data binding is searching for a setUserName method (in your Login.class), which does not exist, throwing an error.

正如您在评论中正确提到的那样,这些方法也应该调用 notifyPropertyChanged(BR.propertyName)

在你的 Login.class 添加

public void setUsername(String username) { 
    this.username = username; 
    notifyPropertyChanged(BR.username); 
} 

public void setPassword(String password) { 
    this.password = password; 
    notifyPropertyChanged(BR.password); 
}