当前位置: 首页 > 知识库问答 >
问题:

在Kotlin 1.6和Android Studio更新后,数据绑定显示错误,但应用程序编译并运行良好

子车芷阳
2023-03-14

我的android studio版本是

Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021

在将IDE和kotlin更新为1.6之后,我打开了一个正在开发的项目,Android Stidio将数据绑定字段标记为红色,显示以下错误:

在文本中:

Cannot access 'androidx.databinding.Observable' which is a supertype of 
'com.cioccarellia.wordbucket.databinding.ActivityMainBinding'. 
Check your module classpath for missing or conflicting dependencies

奇怪的是,一切都在编译,应用程序运行良好,这些错误只是在IDE中,每次我通过数据绑定访问视图时,它们都会弹出。

自从更新android studio以来,我没有改变我的依赖关系。对于这种情况,还有其他人有相同的问题/解决方案吗?

主要活动xml:

<?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>

    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.cioccarellia.wordbucket.activity.MainActivity">

        ...

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

通用构建。格拉德尔:

buildscript {
    ext {
        kotlin_version = "1.6.0"
    }

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        maven { url 'https://jitpack.io' }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.3"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
        classpath "com.github.ben-manes:gradle-versions-plugin:0.39.0"
    }
}

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

申请build.gradle:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"

    id "com.google.gms.google-services"
    id "androidx.navigation.safeargs.kotlin"
    id "com.github.ben-manes.versions"
    id "kotlin-parcelize"
    id "com.google.firebase.crashlytics"
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.cioccarellia.wordbucket"
        minSdk 23
        targetSdk 31

        versionCode 1
        versionName "1.0.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary true
    }

    buildTypes {
        debug {
            versionNameSuffix ".dev"
        }

        release {
            debuggable false

            // Enables code shrinking, obfuscation, and optimization
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    buildFeatures {
        dataBinding true
    }

    sourceSets {
        main.res.srcDirs = [
                "src/main/res",
                "src/main/res-public"
        ]

        android.sourceSets.all {
            java.srcDir("src/$name/kotlin")
        }
    }

    lintOptions {
        // if set to true (default), stops the build if errors are found.
        abortOnError false
        // if true, only report errors.
        ignoreWarnings false
    }
}

dependencies {
    // AndroidX
    implementation "androidx.core:core-ktx:1.7.0"
    implementation "androidx.appcompat:appcompat:1.4.0"
    implementation "androidx.constraintlayout:constraintlayout:2.1.2"
    implementation "com.google.android.material:material:1.6.0-alpha01"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"

    // Lifecycle
    def lifecycle_version = "2.4.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.5"


    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"



    // DI -> Hilt
    implementation "com.google.dagger:hilt-android:2.40.3"
    kapt "com.google.dagger:hilt-compiler:2.40.3"



    // Import the Firebase BoM
    implementation platform("com.google.firebase:firebase-bom:28.4.2")

    // When using the BoM, you don"t specify versions in Firebase library dependencies
    implementation "com.google.firebase:firebase-crashlytics-ktx"
    implementation "com.google.firebase:firebase-analytics-ktx"
    implementation "com.google.firebase:firebase-auth-ktx"
    implementation "com.google.firebase:firebase-firestore-ktx"


    // LeakCanary
    debugImplementation "com.squareup.leakcanary:leakcanary-android:2.7"


    // Logging
    implementation "com.jakewharton.timber:timber:5.0.1"


    // Styles
    implementation 'com.airbnb.android:paris:2.0.1'
    kapt 'com.airbnb.android:paris-processor:2.0.1'


    // JUnit Default Dependencies
    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}

共有2个答案

柯正谊
2023-03-14

更新android studio修复了该问题

缪风史
2023-03-14

尝试重新启动IDE。如果这不起作用,请清理将要构建的项目

编辑:另一个原因是Android Studio将不再支持绑定到直线元素,因此您必须自己指定绑定。

为此,请在应用程序gradle文件中添加BuildFeatures:

android {
compileSdk 31

//This right here
buildFeatures{
    viewBinding true
}

defaultConfig {
    applicationId "com.cioccarellia.wordbucket"
    minSdk 23
    targetSdk 31

    versionCode 1
    versionName "1.0.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary true
}

然后通过将其添加到活动文件来创建新绑定:

//Declare the binding
private lateinit val binding : ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    //Initialize the binding
    binding = ActivityMainBinding.inflate(layoutInflater)

    super.onCreate(savedInstanceState)
    //Change the content view to the root of the binding
    setContentView(binding.root)
}

然后在使用视图时引用绑定:

binding.toolbar.title = bucket.name
 类似资料:
  • 但是当我试图在Netbeans中清理和构建它时,它被这条线噎住了: 错误是:

  • 错误如下: 02-11 13:24:31.561 526 2-5262/?E/AndroidRuntime:致命异常:main process:com.example.vicky.project,pid:5262 java.lang.runtimeException:无法启动活动ComponentInfo{com.example.vicky.project/com.example.vicky.pr

  • 根据这个问题,我在Android Studio中设置了使用Square Wire库的项目。 ->工作良好。 但是,如果我打开一个生成的协议缓冲区文件,那么Square包将无法被识别。 除了在中为项目中的模块添加依赖项之外,我还需要做更多的事情吗?

  • 我正在尝试使用rails创建新项目,但它显示错误。我正在处理mac及其显示错误 $rails新特快专递

  • 我添加Nuget Package Install-Package MvcSiteMapProvider.MVC4 编译器错误消息: CS1705:程序集'MvcSite Map提供者,版本=4.6.18.0,文化=中性,公共密钥令牌=1923abe4657913cc'使用'System. Web. Mvc,版本=4.0.0.0,文化=中性,公共密钥令牌=31bf3856ad364e35',其版本高

  • 最近,我将我的Android Studio从2.2.3更新为2.3.0,项目在2.2.3中运行良好,但在将项目依赖项类路径从2.2.3更新为2.3.0后,从字符串中获取大量缺失的翻译错误。xml。 有人能告诉我为什么会发生这种情况以及如何解决这些错误吗?