android - 第二次打开时,BottomSheetDialogFragment 崩溃
问题描述
之前有人问过类似的问题,但这似乎是一个不同的问题。
使用 Navigation 组件,我定义了一个对话框,如下所示:
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_graph"
app:startDestination="@id/main_fragment">
<fragment
android:id="@+id/main_fragment"
android:name="com.example.app.MainFragment"
android:label="MainFragment" />
<dialog
android:id="@+id/dialog_fragment"
android:name="com.example.app.DialogFragment"
android:label="AddFeeling" />
</navigation>
在MainFragment
我有一个运行的按钮navController.navigate(R.id.dialog_fragment)
。这会很好地打开对话框。当我将对话框片段滑开并再次单击按钮时,应用程序崩溃了。
崩溃是由在对话框中包含一个片段引起的。从技术上讲,我有一个NavHostFragment
对话框片段的内部,但即使在应用程序崩溃中放置一个标准片段。如果我在对话框中没有片段,它工作正常。
这是对话框片段布局。如您所见,该<fragment>
属性是重复项所在的位置:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_sheet_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/dialog_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:navGraph="@navigation/dialog_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
有什么想法可能是什么问题?谢谢
解决方案
与我链接的问题类似,NavHostFragment 的片段也不能有 ID,inflater 会注意到片段有 ID,因此会将其标记为重复。
推荐阅读
- javascript - 由于多个 HTML 文件,DOM 抛出错误
- python - Django 开发:Chrome 警告“更改密码”
- c++ - How to delete a contact from a phonebook program
- javascript - 考虑过滤的选项是可选的,过滤数组
- cassandra - 如何在 cassandra 物化视图中删除一行?
- firebase - 通过 REST API 从 Firebase 实时数据库中删除最后一个节点
- wordpress - 阻止来自服务器名称的访问
- sql - SQL:如何提高视图的 SQL 查询性能?
- java - AbstractProcessor 可以检测带注释的方法是否存在编译错误?
- c - 在 C 中计算大小写字母的问题