首页 > 解决方案 > 第二次打开时,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>

有什么想法可能是什么问题?谢谢

标签: androidandroid-fragmentsandroid-dialogfragmentandroid-navigationandroid-architecture-navigation

解决方案


与我链接的问题类似,NavHostFragment 的片段也不能有 ID,inflater 会注意到片段有 ID,因此会将其标记为重复。


推荐阅读