首页 > 解决方案 > 找不到@Bindable 属性

问题描述

基本上,我有以下课程:

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我得到Unresolved reference: 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)
    }

}

标签: androidandroid-databinding

解决方案


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


推荐阅读