为什么我的数据绑定库不起作用?

Why my Data-Binding library does not working?

我试图从我的 class 获取数据到我的 xml 文件,所以我尝试使用数据绑定。但是数据绑定库不起作用。当我在 xml 文件中写入

   '''plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id("androidx.navigation.safeargs")
    id 'com.google.gms.google-services'
    id "org.jetbrains.kotlin.kapt"
    id 'kotlin-kapt'





}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.cagataysencan.forumfisk_it"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }


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

    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {


    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'


    //Navigation v Google Maps
    implementation("androidx.navigation:navigation-fragment-ktx:2.3.5")
    implementation("androidx.navigation:navigation-ui-ktx:2.3.5")
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // Firebase
    implementation platform('com.google.firebase:firebase-bom:28.2.0')
    implementation  'com.google.firebase:firebase-auth-ktx'
    implementation  'com.google.firebase:firebase-firestore-ktx'
    implementation  'com.google.firebase:firebase-storage-ktx'

    // Picasso
    implementation 'com.squareup.picasso:picasso:2.71828'

    // ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1")
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.3.1")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
    implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.1")
    implementation("androidx.lifecycle:lifecycle-service:2.3.1")
    implementation("androidx.lifecycle:lifecycle-process:2.3.1")
    implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:2.3.1")
    testImplementation("androidx.arch.core:core-testing:2.1.0")


    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.8.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.8.1"

    // RX Java
    implementation "io.reactivex.rxjava2:rxjava:2.2.9"
    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"

    // Glide
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

    // Room
    implementation 'androidx.room:room-rxjava2:2.3.0'
    implementation "androidx.room:room-guava:2.3.0"
    implementation "androidx.room:room-ktx:2.3.0"
    implementation ("androidx.room:room-runtime:2.3.0")
    annotationProcessor "androidx.room:room-compiler:2.3.0"
    kapt("androidx.room:room-compiler:2.3.0")
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1"

}**

请在您的 gradle 文件中匹配一些插件和依赖项以及版本与 kotlin 和数据绑定,您缺少的内容请检查

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

dependencies{
   kapt 'com.android.databinding:compiler:4.0.0'
}


ext.kotlin_version = '1.4.10'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

希望对您有所帮助

删除行:

databinding {
    enabled=true
}

此外,删除 viewBinding,因为如果您已经在使用 dataBinding,则不需要它。. 将 buildFeatures 移出 buildTypes,这样它应该是这样的

android {
    ...
    buildTypes {
    ...
    }

    buildFeatures {
        dataBinding true
    }
}

并且当您想要使用数据绑定时,请确保使用 <layout> 标记将您的布局括起来,以表示该布局符合数据绑定条件。因此,您要绑定的每个 xml 文件都应具有 <layout> 标记,并且可以像这样,例如:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools">

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

       <TextView
...
...
</layout>