java - 无法生成 DataBindingClass(Impl 会生成)

标签 java android androidx

我最近不再使用 ButterKnife使用 android DataBinding , 但其中一个类无法生成。

ActivityMainBindingImpl < 生成良好。

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

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

我有

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

最佳答案

你试过像这样添加数据标签吗?

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

如果我省略了数据标签,至少我的绑定(bind)类会变得一团糟,尽管有时重启 Android Studio 也会产生奇怪的效果。

(抱歉,我更愿意将这个问题添加为评论,但我没有足够的声誉)

关于java - 无法生成 DataBindingClass(Impl 会生成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60393275/

相关文章:

java - 如何在启动画面上加载主屏幕?

android - 如何使用带有底部导航 View 的全屏 fragment ?

android - 在 android 5 上膨胀类 android.webkit.WebView 时出错

android - 是什么取代了 AndroidX 中的 appcompat-v7

java - JTextField 永久位置/位置?

java - 如何从 url 的最后一个路径中分割剩余路径

android - 如何从 EditText 获取文本?

java - 线程正在阻塞执行

Java线程并发

java - 使用 contains 查找二维数组中的元素