Android @未找到可绑定属性

Android @未找到可绑定属性,android,android-databinding,Android,Android Databinding,基本上,我有以下课程: class SignInRequest: BaseObservable() { @Bindable var email: String = "" set(value) { field = value notifyPropertyChanged(BR.email) } @Bindable var password: String = "" se

基本上,我有以下课程:

class SignInRequest: BaseObservable() {

    @Bindable
    var email: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.email)
        }

    @Bindable
    var password: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.password)
        }
}
当我专注于
BR.email
时,我得到了
未解析的引用:email
(与
BR.password
相同)。奇怪的是,如果我打开自动生成的BR类,这些变量就在那里。此外,项目构建正确,应用程序按示例运行。只是由于某些原因,Android Studio无法访问这些变量

这是我的模块
build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "it.fisbapp"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

    dataBinding {
        enabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    // DEFAULT
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    // FIREBASE
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.1.1'
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.google.firebase:firebase-auth:19.2.0'

    // DAGGER 2
    implementation 'com.google.dagger:dagger:2.22.1'
    implementation 'com.google.dagger:dagger-android:2.22.1'
    implementation 'com.google.dagger:dagger-android-support:2.22.1'
    kapt 'com.google.dagger:dagger-android-processor:2.22.1'
    kapt 'com.google.dagger:dagger-compiler:2.22.1'

    // KOTLIN ARROW
    implementation "io.arrow-kt:arrow-core-data:0.9.0"

    // ARCHITECTURE COMPONENTS
    kapt "com.android.databinding:compiler:3.2.0-alpha10"
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-runtime:2.1.0'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.1.0'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha04"
}

apply plugin: 'com.google.gms.google-services'
渐变版本:5.4.1

Android Studio版本(适用于Linux):3.5.3

编辑

我忘了提到我已经尝试清理和重建项目,并使缓存无效并重新启动

布局:

    <?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="viewModel" type="it.fisbapp.ui.welcome.viewmodel.SignInViewModel"/>
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.welcome.fragment.SignInFragment">

        <ScrollView
            android:fillViewport="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="70dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <androidx.constraintlayout.widget.Guideline
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" app:layout_constraintGuide_begin="24dp"
                    android:id="@+id/guideline_left"/>

                <androidx.constraintlayout.widget.Guideline
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:id="@+id/guideline_right"
                    app:layout_constraintGuide_end="24dp"/>

                <EditText
                    android:id="@+id/email"
                    android:inputType="textEmailAddress"
                    android:hint="@string/sign_in_layout_edit_text_email_hint"
                    android:text="@={viewModel.signInRequest.email}"
                    app:error="@{viewModel.signInRequest.emailError}"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="100dp"
                    app:layout_constraintEnd_toStartOf="@+id/guideline_right"
                    app:layout_constraintStart_toStartOf="@+id/guideline_left"
                    app:layout_constraintTop_toTopOf="parent" />

                <EditText
                    android:id="@+id/password"
                    android:inputType="textPassword"
                    android:hint="@string/sign_in_layout_edit_text_password_hint"
                    android:text="@={viewModel.signInRequest.password}"
                    app:error="@{viewModel.signInRequest.passwordError}"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="@+id/email"
                    android:layout_marginTop="100dp"
                    app:layout_constraintEnd_toStartOf="@+id/guideline_right"
                    app:layout_constraintStart_toStartOf="@+id/guideline_left"/>

                <Button
                    android:id="@+id/signIn"
                    android:text="@string/sign_in_layout_button_sign_in"
                    android:onClick="@{() -> viewModel.onSignIn()}"
                    app:gone="@{safeUnbox(viewModel.isLoading)}"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="@+id/password"
                    android:layout_marginTop="100dp"
                    app:layout_constraintEnd_toStartOf="@+id/guideline_right"
                    app:layout_constraintStart_toStartOf="@+id/guideline_left"/>

                <TextView
                    android:id="@+id/gotoSignUp"
                    android:text="@string/sign_in_layout_label_sign_up"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toStartOf="@+id/guideline_right"
                    app:layout_constraintStart_toStartOf="@+id/guideline_left"
                    app:layout_constraintTop_toBottomOf="@+id/signIn"
                    android:layout_marginTop="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="@{() -> viewModel.onGotoSignUp()}"/>

                <TextView
                    android:id="@+id/gotoForgotPwd"
                    android:text="@string/sign_in_layout_label_reset_password"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toStartOf="@+id/guideline_right"
                    app:layout_constraintStart_toStartOf="@+id/guideline_left"
                    app:layout_constraintTop_toBottomOf="@+id/gotoSignUp"
                    android:layout_marginTop="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="@{() -> viewModel.onGotoResetPwd()}"/>

                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toStartOf="@+id/guideline_right"
                    app:layout_constraintStart_toStartOf="@+id/guideline_left"
                    app:layout_constraintTop_toBottomOf="@+id/gotoForgotPwd"
                    android:layout_marginTop="20dp"
                    app:visible="@{safeUnbox(viewModel.isLoading)}"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

        </ScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

视图模型:

class SignInViewModel(val authRepository: AuthRepository): BaseViewModel() {

    val signInRequest = SignInRequest()
    val onSignin = MutableLiveData<Boolean>()
    val onError = MutableLiveData<String>()
    val onGotoSignUp = MutableLiveData<Boolean>()
    val onGotoResetPwd = MutableLiveData<Boolean>()
    val isLoading = MutableLiveData<Boolean>().apply { postValue(false) }

    fun onSignIn() {
        isLoading.postValue(true)
        viewModelScope.launch {
            if(signInRequest.isValid()) {
                val email = signInRequest.email
                val pwd = signInRequest.password
                authRepository
                    .signIn(email, pwd)
                    .fold(
                        ifLeft = { onError.postValue(it.errMsg) },
                        ifRight = { onSignin.postValue(true) }
                    )
            }
            isLoading.postValue(false)
        }
    }

    fun onGotoSignUp() {
        onGotoSignUp.postValue(true)
    }

    fun onGotoResetPwd() {
        onGotoResetPwd.postValue(true)
    }

}
class-SignInViewModel(val-authRepository:authRepository):BaseViewModel(){
val signInRequest=signInRequest()
val onSignin=MutableLiveData()
val onError=MutableLiveData()
val onGotoSignUp=MutableLiveData()
val onGotoResetPwd=MutableLiveData()
val isLoading=MutableLiveData().apply{postValue(false)}
fun onSignIn(){
isLoading.postValue(true)
viewModelScope.launch{
if(signInRequest.isValid()){
val email=signInRequest.email
val pwd=signInRequest.password
authRepository
.signIn(电子邮件,pwd)
.折叠(
ifLeft={onError.postValue(it.errMsg)},
ifRight={onSignin.postValue(true)}
)
}
isLoading.postValue(false)
}
}
趣味英语报名{
onGotoSignUp.postValue(true)
}
fun OnMotoreSetpwd(){
OnGetOresetPwd.postValue(true)
}
}

您需要在布局文件的相应属性中提及sme变量。
这是数据绑定的实际流程。这样,您的布局nd模型和视图模型可以相互交互。

您可以尝试替换这一行吗:

kapt "com.android.databinding:compiler:3.2.0-alpha10"
为此:

kapt 'com.android.databinding:compiler:3.1.4'
另外,您也可以尝试使用ObservaleField

public class User {
public static ObservableField<String> name = new ObservableField<>();
public static ObservableField<String> email = new ObservableField<>();

public ObservableField<String> getName() {
    return name;
}

public ObservableField<String> getEmail() {
    return email;
}
}
公共类用户{
public static observefield name=new observevefield();
公共静态ObserveField电子邮件=新ObserveField();
公共observeField getName(){
返回名称;
}
public observeField getEmail(){
回复邮件;
}
}

它很可能无法构建,而
BR
无法解决-但是
BR
一旦成功构建,它应该能够解决

尝试注释掉
//notifyPropertyChanged()
生成项目,然后再次取消注释


我认为您可以删除
com.android.databinding:compiler:3.2.0-alpha10
,因为配置
databinding{enabled=true}
添加了它需要的所有依赖项。

在您的例子中,我认为在xml布局中绑定属性
email
密码应该有问题。你也可以发布相应的xml文件吗?当然可以。我觉得布局还可以。此外,应用程序运行正常,因此我猜它与Android Studio有关,因为某些原因无法访问构建文件夹。也许我错了,但在我看来,将viewmodel声明为布局中的变量并访问viewmodel的变量(LiveData、ObservableProperty等)是一种相当常见的模式通过数据绑定替换依赖项不起作用,关于可观察字段,是的,它应该起作用。但是我更想理解为什么当前的实现不起作用。我对
notifyPropertyChanged()
@Bindable
注释进行了注释,并从自动生成的BR类中删除了变量。未加注释,重新构建,它已返回。除了android studio报告了一个不存在的错误之外,一切都正常工作。