Android数据绑定库-未解决的参考

阿列克谢·马可夫(Alexey Markov)

我正在使用Kotlin并尝试使用新的数据绑定库,但是出现xxxBinding类的未解决引用错误

顶层 build.gradle

buildscript {
    ext.kotlin_version = '1.2.30'
    ext.gradle_version = '3.1.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程式 build.gradle

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

android {
    buildToolsVersion "27.0.3"
    compileSdkVersion 27
    defaultConfig {
        applicationId "ru.vstu.clustermonitor"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

repositories {
    maven {
        url 'https://dl.bintray.com/terrakok/terramaven/'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'

    implementation 'com.android.support:recyclerview-v7:27.1.1'                 // RecycleView
    implementation 'com.android.support:cardview-v7:27.1.1'                     // CardView

    implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha01'  // Navigation Architecture Components (remove)

    kapt "com.android.databinding:compiler:$gradle_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

MainActivity.onCreate

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)        
    val binding : ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
}

ActivityMainBinding应该生成,但不是。有什么办法可以解决这个问题?

阿列克谢·马可夫(Alexey Markov)

问题出在使用ConstrainLayout而不是layout

使用数据绑定时,根布局必须是“布局”类型

<?xml version="1.0" encoding="utf-8"?>
<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>
        <variable
            name="MyVariable"
            type="com.mycompany.myproject.MyType"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Views.Fragments.QueueFragment"
        android:background="#fff">

        ...

    </android.support.constraint.ConstraintLayout>
</layout>

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <data>
        <variable
            name="MyVariable"
            type="com.mycompany.myproject.MyType"/>
    </data>

    ...        
</layout>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未解决的参考:android

未解决的参考layoutInflater Android视图绑定

Android Studio Kotlin数据绑定:BR上未解决的参考

Android Kotlin:未解决的参考

未解决的参考:构建Corda时绑定

未解决的参考:Kotlin Android中的ContextCompat

Android未解决的参考:findNavController错误

未解决的参考:viewModelScope-Android KTX

Android错误:未解决的参考:utils

Android / Kotlin:未解决的参考:木材

Android导航组件未解决的参考

Android Studio未解决的参考。项目编译

未解决的参考:BR(Android Studio)

Android Studio和Kotlin:未解决的参考:也

未解决的参考:requireLensFacing | Android Studio

未解决的参考:viewModelScope - Kotlin Android

未解决的参考:FontAwesomeIconView - 我应该导入什么库?

android studio, kotlin 中未解决的参考 android

未解决的参考:新Flutter项目上的android

Android,Anko cardView不可用-未解决的参考:cardView

Android Studio:未解决的参考:box_three_text

Android ActivityResult API未解决的参考错误registerForActivityResult

未解决的参考,测试范围错误-Android Studio 3.5

Android Studio 3.4.2-未解决的参考v7

Android Studio 3.4.1,Kotlin“未解决的参考:mutableListOf”

Android Studio-Kotlin Project的“未解决的参考”问题

Android Studio中未解决的参考问题

Android Gradle Kotlin Dsl 中未解决的参考问题

编译和使用Live555库-Windows和MinGW(未解决的参考)