首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视图绑定不生成

视图绑定不生成
EN

Stack Overflow用户
提问于 2020-02-29 12:03:28
回答 8查看 18.2K关注 0票数 33

我目前正在试用新的ViewBindings,但我不会让它们开始工作。我上Android Studio 3.6.1了。我刚刚创建了一个新项目,并将viewBinding.enabled = true添加到模块build.gradle中。但是当我试图访问MainActivityBinding类时,它说它不能解析这个符号。Autocomplete没有找到任何类似于绑定类的东西。我也尝试了一个不同的项目使用Kotlin,但没有成功。AS4.0也帮不上忙。要生成ViewBinding类,我需要做什么?

我的build.gradle

代码语言:javascript
复制
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'
}
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2020-03-03 14:21:06

直到我意识到绑定是从XML文件("fragment_home.xml")而不是类("HomeFragment.kt")获取它们的名称,我才找到我的fragment_home.xml文件。所以我在HomeFragmentBinding找不到他们,但我在FragmentHomeBinding.找到他们

我发现将每个ViewBinding看作是作为该XML文件的委托创建的助手单例是很有帮助的。

(编辑以删除过时的分级材料)

票数 133
EN

Stack Overflow用户

发布于 2021-11-11 17:49:28

布局的根标记必须是<layout>。确保这不是你的案子。

必须更改我的原始布局XML文件

代码语言:javascript
复制
<?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>

代码语言:javascript
复制
<?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>
票数 5
EN

Stack Overflow用户

发布于 2022-01-06 13:44:29

在我的例子中,我试图绑定视图

添加build.gradle (模块)

代码语言:javascript
复制
buildFeatures{
 dataBinding true
 viewBinding true
}

前一个是

代码语言:javascript
复制
viewBinding{
enabled = true
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60464962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档