【问题标题】:Why my Data-Binding library does not working?为什么我的数据绑定库不起作用?
【发布时间】:2021-09-23 14:28:29
【问题描述】:

我正在尝试将数据从我的班级获取到我的 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"

}**

【问题讨论】:

  • "当我在我的 xml 文件中写入
  • 没有。我无法在 xml 文件中打开像 这样的标签。这给我一个错误:“这里不允许元素数据。”
  • 你能显示任何代码来表明你是如何尝试生成 xml 的吗?

标签: java android kotlin data-binding


【解决方案1】:

删除线:

databinding {
    enabled=true
}

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

android {
    ...
    buildTypes {
    ...
    }

    buildFeatures {
        dataBinding true
    }
}

当您想使用 dataBinding 时,请确保使用 <layout> 标记将您的布局括起来,以表示该布局符合 dataBinding 的条件。因此,您要绑定的每个 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>

【讨论】:

    【解决方案2】:

    请在您的 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"
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2021-07-24
      • 2011-01-16
      • 2021-04-09
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多