26

我目前正在尝试新的 ViewBindings,但我没有让它们工作。我在Android Studio 3.6.1。我刚刚创建了一个新项目并添加viewBinding.enabled = true到我的模块build.gradle中。但是当我尝试访问MainActivityBinding该类时,它说它无法解析该符号。自动完成没有找到任何类似于绑定类的东西。我还尝试了一个使用 Kotlin 的不同项目,但没有成功。AS4.0也无济于事。我需要做什么来生成ViewBinding类?

我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    viewBinding {
        enabled = true
    }

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 29
        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'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
4

6 回答 6

104

我无法找到我的ViewBinding文件,直到我意识到绑定是以XML文件(“fragment_home.xml”)而不是类(“HomeFragment.kt”)命名的。所以我找不到他们,HomeFragmentBinding但我找到了他们FragmentHomeBinding.

我发现将每个 ViewBinding 视为已创建为该 XML 文件的委托的辅助单例很有帮助。

(编辑删除过时的 Gradle 东西)

于 2020-03-03T14:21:06.067 回答
1

布局的根标签必须是<layout>. 确保不是你的情况。

不得不更改我原来的布局 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/welcome_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Hello World!" />

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/welcome_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Hello World!" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>
于 2021-11-11T17:49:28.943 回答
1

就我而言,我试图绑定 View

添加了 build.gradle (模块)

buildFeatures{
 dataBinding true
 viewBinding true
}

上一个是

viewBinding{
enabled = true
}
于 2022-01-06T13:44:29.230 回答
0

我刚刚更改了布局的名称,它工作得很好

于 2020-04-26T11:21:18.273 回答
0

如果您手动输入了片段/布局名称,请检查布局名称的关键字的拼写,例如“片段”,其中的错误会生成具有相同名称的绑定文件

于 2022-02-24T22:52:23.290 回答
-3

确保您已完成的几件事:

  • 确保你有一个格式正确的布局文件,顶部的布局标签声明你的变量
  • 尝试 gradle 同步

我还没有检查,但我知道目前我的团队的一些成员报告绑定类在升级到 3.6 后不再在 Android Studio 中得到解决,但是该项目仍然构建良好,所以它们实际上在那里,可能是一个错误与 Android 工作室?

于 2020-03-02T12:16:08.040 回答