首页 > 解决方案 > 无法生成 DataBindingClass(Impl 确实生成)

问题描述

我最近从 using 切换ButterKnife到 using android DataBinding,但是其中一个类无法生成。

ActivityMainBindingImpl< 生成良好。

但是ActivityMainBinding丢失了,它也表明在 Impl 类本身中找不到它。

到目前为止,我已经尝试了您可以通过多个搜索引擎在互联网上找到的所有内容。(使缓存重建更新等无效。)也许这有点小众,我希望你们中的一些人以前遇到过它?

我有

dataBinding {
    enabled = true
}

需要注意的是,我使用的是 androidx 而不是旧的支持库。

它可能会在某个地方默默地失败,但我已经尝试过<data>在我的布局文件中使用和不使用任何元素。

<layout>我也像这样正确地包装我的布局:

<layout 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">

看起来很奇怪,它可以很好地生成 Impl 类,但另一个不存在。

我也进入了生成它的文件夹,只有 Impl 类可见。

完整的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.activities.MainActivity"
    tools:layout_editor_absoluteY="25dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/toolbar_container_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_container_layout">
        <!-- This is a comment  #001C40 -->

        <LinearLayout
            android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/dashboard"
                android:layout_width="match_parent"
                android:layout_height="700dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar_container_layout">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/dashboard_header"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/logo_container"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginEnd="8dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                        <ImageView
                            android:id="@+id/facta_logo"
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:adjustViewBounds="true"
                            android:scaleType="fitCenter"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.0"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintVertical_bias="0.0"
                            app:srcCompat="@drawable/facta_logo" />
                    </androidx.constraintlayout.widget.ConstraintLayout>

                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.gridlayout.widget.GridLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:horizontalSpacing="16dp"
                    android:verticalSpacing="16dp"
                    app:columnCount="2"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/dashboard_header"
                    app:rowCount="4">


                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />


                </androidx.gridlayout.widget.GridLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>

        </LinearLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

错误是

   Cannot resolve symbol 'ActivityMainBinding'

我的 XML 布局在哪里activity_main.xml

标签: javaandroidandroidx

解决方案


您是否尝试过像这样添加数据标签?

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewmodel"
            type="com.myapp.data.ViewModel" />
    </data>
    <ConstraintLayout... /> <!-- UI layout's root element -->
</layout>

如果我省略数据标签,至少我的绑定类会搞砸,尽管重新启动 Android Studio 有时也会产生奇怪的效果。

(对不起,我宁愿将此问题添加为评论,但我没有足够的声誉)


推荐阅读