Android Studio 在启用视图绑定后给出错误和警告。怎么解决?

问题描述 投票:0回答:13

在我的项目中启用视图绑定后出现错误。

我尝试重新同步项目,使缓存失效。 我的应用程序 gradle 文件:

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.learning.aboutme"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    viewBinding.enabled = true

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

我的项目gradle文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
     ext.kotlin_version = '1.3.72'
     repositories {
          google()
          jcenter()

     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.3'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

          // 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
}

我的活动 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/name_text"
        style="@style/name_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAlignment="center" />

    <EditText
        android:id="@+id/nickname_edit"
        style="@style/name_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/name_it_what_is_your_nickname"
        android:inputType="textPersonName"
        android:textAlignment="center" />

    <Button
        android:id="@+id/done_button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/layout_margin"
        android:fontFamily="@font/roboto"
        android:text="@string/done" />

    <TextView
        android:id="@+id/nickname_text"
        style="@style/name_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/star_Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_margin"
        android:contentDescription="@string/yellow_star"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <ScrollView
        android:id="@+id/bio_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="@dimen/spacing_multiplier"
            android:text="@string/bio"
            android:textColor="@android:color/black" />
    </ScrollView>
</LinearLayout>

这里 android studio 给出了关于

android:contentDescription
textAlignment
lineSpacingMultiplier
text
等的警告

这是我的主要 Activity.kt 文件:

package com.learning.aboutme

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.learning.aboutme.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_main)
    }

    private fun addNickname(view: View) {

    }
}

这里 android studio 说:无法访问

android.viewbinding.ViewBinding
,它是
com.learning.aboutme.databinding.ActivityMainBinding
的超类型。开
ActivityMainBinding.inflate()

我尝试创建一个新项目并将文件移到那里,但出现相同的错误(尝试移动

activity_main.xml
和相同的错误。

如何解决这个问题?

android kotlin gradle android-viewbinding
13个回答
44
投票

对我来说,添加后错误就消失了

implementation 'com.android.databinding:viewbinding:4.0.1'

build.gradle
文件。

此依赖项包含 AndroidStudio 无法访问的类

android.viewbinding.ViewBinding


12
投票

就我而言,以下步骤(清除缓存)确实有帮助:

第 1 步:打开终端并转到终端中的项目路径。
第 2 步:可选点击此命令

git clean -xfd
或安全变体
git clean
请小心
-xfd
这会删除未跟踪的文件并且
.gitignore files

第 3 步:转到:文件 -> 使缓存无效/重新启动。


10
投票

昨天面临同样的问题,确切的错误。对我来说解决方案是

  • 第 1 步 - 删除 .Idea 文件夹
  • 第2步-关闭android studio
  • 第3步-使用相同的项目路径重新打开android studio(这将生成新的 .创意文件夹)

错误消失了

确定

  1. android.useAndroidX=true。 在 gradle.properties 文件内
  2. 应用程序 build.gradle 内部
 buildFeatures {
       viewBinding true
 }
  1. 始终使用小写的文件夹结构,后者仅允许类名大写
  2. 如果您在 xml 或布局标签顶部使用任何版权注释而没有变量标签,或者我们可以说没有任何数据,则删除它并尝试清理/重建项目

2
投票

我从这里找到了解决方案:https://cdmana.com/2021/05/20210511224308742x.html。只需添加

gradle.properties

//...
android.enableD8=true
android.injected.testOnly=false

希望能解决您的问题


2
投票

与该错误有同样的问题

android.viewbinding.ViewBinding which is a supertype of com.learning.aboutme.databinding.ActivityMainBinding. On ActivityMainBinding.inflate()

我所做并解决的问题是从 build.grade 模块中删除

viewBinding{
   enabled = true
}

然后同步它,重新添加从模块中删除的代码并再次同步,瞧!这为我解决了。感谢所有其他回复


1
投票

今天,当我在片段中使用 viewBinding 时,我遇到了这个确切的错误,所以有帮助的是

  1. 清理并重建项目。
  2. 使缓存无效并重新启动。

不知何故,无效缓存并重新启动具体解决了该问题。


0
投票

为我解决的错误: 在 gradle.properties(Project Propoerties) 文件中添加以下行:

android.useAndroidX=true


0
投票

您可以尝试这个解决方案:

在 gradle 文件中添加“multiDexEnabled true”,如下所示

android {
compileSdk 30

defaultConfig {
    applicationId "com.example.test"
    minSdk 19
    targetSdk 30
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

    testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}}

0
投票

我在 build.gradle 中添加依赖项

  • 实现“com.android.databinding:viewbinding:4.0.1”

0
投票

我遇到了同样的问题,我通过执行以下步骤解决了它:

1- 转到 build.gradle 文件并删除

 buildFeatures { viewBinding = true }
如果你已经把它放在那里了
2-同步
3-清理并重建项目。
4-把viewBinding再次放回去
 buildFeatures { viewBinding = true}

5-再次同步


0
投票

对我来说,添加后错误就消失了

implementation 'com.android.databinding:viewbinding:7.3.1'
到 build.gradle 文件。

此依赖项包含

android.viewbinding.ViewBinding
,如果没有此依赖项,android studio 可能无法访问。


0
投票

对我来说,发生这种情况是因为我将布局包装到

<layout>
标签中。仅当您使用数据绑定时才需要此标记,并且单独与视图绑定一起使用时会导致问题。


0
投票

只需保持设置不变..尝试从路径“C:\Users”您的用户名“.gradl 中删除数据绑定模块

© www.soinside.com 2019 - 2024. All rights reserved.