android - 使用 View 绑定(bind)时如何解决 'data binding is not enabled' 错误

标签 android kotlin android-jetpack android-viewbinding

我正在使用 View 绑定(bind),为此我使用了以下代码。

buildFeatures{ viewBinding true }
我得到了这个错误:
> [databinding] {"msg":"Found \u003clayout\u003e but data binding is not
> enabled.\n\nAdd buildFeatures.dataBinding \u003d true to your
> build.gradle to enable
> it.","file":"C:\\Users\\akash\\AndroidStudioProjects\\NavigationSafeArgs\\app\\src\\main\\res\\layout\\fragment_home.xml","pos":[]}
在阅读了抛出的错误后,我认为它是说启用 dataBinding所以我加了dataBinding true太内了buildFeatures .仍然会引发相同的错误。
这是我的 fragment_home.xml 代码;
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".HomeFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/home_screen"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login" />
</LinearLayout>
</layout>

   

最佳答案

一起启用 dataBinding 和 viewBinding 解决了我的问题。

 buildFeatures {
        viewBinding true
        dataBinding true
 }
我还在插件中添加了 kotlin-kapt
plugins {
     id 'kotlin-kapt'
}

关于android - 使用 View 绑定(bind)时如何解决 'data binding is not enabled' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66459260/

相关文章:

android - 如何使用导航组件处理 fragment 内的向上按钮

Jetpack Compose 中的 android:autoSizeTextType

android - Eclipse中未定义的外部引用

android - 如何在不缩放可绘制的情况下增加自定义 View 扩展按钮的填充?

gradle - 使用 Gradle 在 Kotlin 服务器和客户端项目之间共享代码

java - Kotlin 中具有现有 java.util.Predicate 实例的过滤器集合

android - 使用 Android 导航组件的身份验证流程

Android Studio 在进行 NDK 开发时卡在 macOS 上

java - 检查 editText 中的文本是否已更改

android - Kotlin 数据类 |添加我自己的变量而不是 JSON 键