如何在实现双向数据绑定时导入 BR class?导入 BR 在我的代码中显示错误

How to import BR class while implementing two way data binding? Importing BR shows error in my code

我正在继承 BaseObservable class。在 'notifyPropertyChanged' 方法中,当我传递整数 fieldId 时,'BR' class 显示红色错误。 我正在尝试实现双向数据绑定。

ViewModel.java

package com.example.test;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class ViewModel extends BaseObservable {
String username;
String password;

@Bindable
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
    notifyPropertyChanged(BR.username);
}
@Bindable
public String getPassword() {
    return password;
}

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

我找到问题了。问题出在 IDE 的最新版本中,即 Android Studio 3.3 。它在版本 3.2.1 中运行良好。我希望 Google 尽快解决这个问题。

在您的 java 域 class 中导入 androidx.databinding.library.baseAdapters.BR。

在某些情况下,即使您不导入它,无论它显示错误,代码都会通过。但导入后,错误将消失。

Android Studio 3.5.3