在布局标签中加载片段会导致 "Error inflating class layout"
Loading a fragment within a layout-tag leads to "Error inflating class layout"
我创建了一个片段,我将其合并到我的 main-activity 的 XML 中。我喜欢使用数据绑定。
这是主要的代码-activity 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>
如上所示,代码导致编译时错误“Error inflating class layout”:
2021-05-22 15:13:44.828 23740-23740/com.example.fragmentdemo2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fragmentdemo2, PID: 23740
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentdemo2/com.example.fragmentdemo2.MainActivity}: android.view.InflateException: Binary XML file line #5 in com.example.fragmentdemo2:layout/activity_main: Binary XML file line #5 in com.example.fragmentdemo2:layout/activity_main: Error inflating class layout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
如果我删除顶级 - 标签,那么一切都将按预期正常运行。
这里出了什么问题?如何修复“充气错误”?
据我所知,您缺少 ViewBinding。您可以通过在 build.gradle 文件中将 viewBinding
构建选项设置为 true
来在 Gradle 中启用 ViewBinding/DataBinding,如下所示:
android {
buildFeatures{
viewBinding = true
}
}
我创建了一个片段,我将其合并到我的 main-activity 的 XML 中。我喜欢使用数据绑定。
这是主要的代码-activity 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>
如上所示,代码导致编译时错误“Error inflating class layout”:
2021-05-22 15:13:44.828 23740-23740/com.example.fragmentdemo2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fragmentdemo2, PID: 23740
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentdemo2/com.example.fragmentdemo2.MainActivity}: android.view.InflateException: Binary XML file line #5 in com.example.fragmentdemo2:layout/activity_main: Binary XML file line #5 in com.example.fragmentdemo2:layout/activity_main: Error inflating class layout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
如果我删除顶级 - 标签,那么一切都将按预期正常运行。
这里出了什么问题?如何修复“充气错误”?
据我所知,您缺少 ViewBinding。您可以通过在 build.gradle 文件中将 viewBinding
构建选项设置为 true
来在 Gradle 中启用 ViewBinding/DataBinding,如下所示:
android {
buildFeatures{
viewBinding = true
}
}