基本上,我有以下课程:
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无法访问这些变量。
这是我的模块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>ViewModel:
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)
}
}发布于 2020-01-12 15:22:50
您需要在布局文件中提到中小企业变量,以对应的属性。这是数据绑定的实际流程。这样您的布局、模型和视图模型就可以相互交互。
发布于 2020-01-12 15:24:38
你能试着替换这一行吗?
kapt "com.android.databinding:compiler:3.2.0-alpha10"在这方面:
kapt 'com.android.databinding:compiler:3.1.4'另外,您还可以尝试使用ObservableField。
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;
}
}发布于 2020-01-12 18:18:51
很可能它不会构建,而BR不会解决-但是BR应该解决,一旦它成功构建.
尝试注释掉// notifyPropertyChanged(),构建项目,然后再取消注释。
我认为您可以完全删除com.android.databinding:compiler:3.2.0-alpha10,因为配置dataBinding {enabled = true}添加了它所需的所有依赖项。
https://stackoverflow.com/questions/59705088
复制相似问题