Android 在布局标记中加载片段会导致;膨胀类布局时出错“;

Android 在布局标记中加载片段会导致;膨胀类布局时出错“;,android,android-jetpack,Android,Android Jetpack,我创建了一个片段,并将其合并到我主要活动的XML中。我喜欢使用数据绑定 以下是主要活动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

我创建了一个片段,并将其合并到我主要活动的XML中。我喜欢使用数据绑定

以下是主要活动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=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/contentFragment"
            android:name="com.example.fragmentdemo2.ContentFragment" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
如果我移除顶层标记,那么一切都会正常工作,正如预期的那样


这里出了什么问题?“错误膨胀”如何修复?

据我所见,您缺少ViewBinding。通过在build.Gradle文件中将
ViewBinding
build选项设置为
true
,可以在Gradle中启用ViewBinding/DataBinding,如下所示:

    android {
    buildFeatures{
    viewBinding = true
    }
    }

是否启用了ViewBinding?@esQmo_uu就是这样。非常感谢。如果你愿意,那么把你的评论变成一个答案。我会接受的。
    android {
    buildFeatures{
    viewBinding = true
    }
    }