Android Studio 未生成视图绑定

muk1a3rh  于 2022-11-16  发布在  Android
关注(0)|答案(8)|浏览(176)

我正在尝试新的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'
}
imzjd6km

imzjd6km1#

我找不到我的ViewBinding文件,直到我突然意识到绑定是从XML文件(“fragment_home.xml”)而不是从类(“HomeFragment.kt”)中获取它们的名称。
我发现将每个ViewBinding看作是作为XML文件的委托创建的helper singleton是很有帮助的。
(编辑以删除过时的Gradle内容)

nhhxz33t

nhhxz33t2#

布局的根标签必须是<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>
vnjpjtjt

vnjpjtjt3#

在我的案例中,我尝试系结View
添加了build.gradle(模块)

buildFeatures{
 dataBinding true
 viewBinding true
}

前一个是

viewBinding{
enabled = true
}
im9ewurl

im9ewurl4#

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

ddrv8njm

ddrv8njm5#

如果你已经手动输入了片段/布局名称,请检查布局名称的关键字的拼写,如“fragment”,一个错误将生成一个同名的绑定文件

wz3gfoph

wz3gfoph6#

确保已在模块Gradle文件中添加构建功能

android {

  buildFeatures {
      viewBinding true
  }
}

你可能正在处理ABCFragment,但是它的布局可能被命名为small_mistake. xml,那么你的BindUtil将如下所示。

SmallMistakeViewBinding
7jmck4yq

7jmck4yq7#

此答案写在
11.11.2022
为了生成视图绑定,这里是解决方案:

android {

buildFeatures {
        viewBinding true
    }

}

将上述代码添加到应用程序模块中的android标签内。
你的前布局文件将被生成,当你创建新的布局时,它们将被看到...所以它只对新的布局文件起作用...你不需要在每个xml中添加布局标记...

t1rydlwq

t1rydlwq8#

有几件事要确保你已经做了:

  • 确保布局文件的格式正确,布局标记位于顶部,用于声明变量
  • 尝试Gradle同步

我还没有检查,但我知道目前我的团队的一些成员已经报告绑定类不再是在Android工作室中解决,因为升级到3.6,但该项目仍然建立良好,所以他们实际上存在,可能是Android工作室的一个错误?

相关问题