Bottom Sheet Fragment 第二次调用时出错

Bottom Sheet Fragment causes error when called the second time

我有一个 class BottomSheetFragment extends BottomSheetDialogFragment,它包含子片段,我从片段中调用它。 问题是,当我第二次调用它时,我的应用程序崩溃并出现 Binary XML file line #69: Duplicate id 0x7f090071, tag null, or parent id 0xffffffff with another fragmenterror.

底部 sheet class:

public class BottomSheetFragment extends BottomSheetDialogFragment {
    public BottomSheetFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);

        return v;
    }


}

在其 XML 中包含一个子片段 public class CreateNotesFragment extends android.support.v4.app.Fragment

BottomSheetFragment class 在按钮单击时在另一个片段中调用,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_works, container, false);

        createNew = view.findViewById(R.id.add_file_btn);


        createNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity)getActivity();
                mainActivity.showBottomSheetDialogFragment();
            }
        });


        return view;
    }

这里是 MainActivity 中显示底部的方法 sheet:

public void showBottomSheetDialogFragment() {
            bottomSheetFragment = new BottomSheetFragment();
            bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
    }

我假设由于某种原因第二次调用 BottomSheetFragmentCreateNotesFragment 不再存在,但我该如何修复它?

谢谢。


更新
这是 fragment_bottom_sheet.xml

中的第 69 行
<fragment
            android:id="@+id/notes_fragment"
            android:name="com.app.parsec.secondary_fragments.CreateNotesFragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider9"
            tools:layout="@layout/fragment_create_notes" />

和整个XML确保没有ID重复:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".secondary_fragments.BottomSheetFragment"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/wasd">


    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/notes_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:drawableTop="@drawable/ic_note"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="НОТЫ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toStartOf="@+id/text_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/text_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_t"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ТЕКСТ"
            app:layout_constraintEnd_toStartOf="@+id/project_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/notes_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/project_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_shape_1"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ПРОЕКТ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/text_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/divider9"
            android:layout_width="0dp"
            android:layout_height="3dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_btn" />

        <fragment
            android:id="@+id/notes_fragment"
            android:name="com.app.parsec.secondary_fragments.CreateNotesFragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider9"
            tools:layout="@layout/fragment_create_notes" />


    </android.support.constraint.ConstraintLayout>
</FrameLayout>

我还注意到,如果我从 XML 中删除这个片段,我可以打开 BottomSheetFragment 而不会出现任何崩溃,所以问题一定出在子片段上。


解决了
解决方案是动态地将片段添加到片段中,而不是通过 XML。所以,我的 BottomSheetFragment 现在看起来像这样:

public class BottomSheetFragment extends BottomSheetDialogFragment {
    public BottomSheetFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_here, new CreateNotesFragment()).commit();
        return v;
    }


}
<FrameLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".secondary_fragments.BottomSheetFragment"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/wasd">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/notes_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:drawableTop="@drawable/ic_note"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="НОТЫ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toStartOf="@+id/text_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/text_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_t"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ТЕКСТ"
            app:layout_constraintEnd_toStartOf="@+id/project_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/notes_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/project_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_shape_1"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ПРОЕКТ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/text_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/divider9"
            android:layout_width="0dp"
            android:layout_height="3dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_btn" />


        <FrameLayout
            android:id="@+id/fragment_here"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider9">

        </FrameLayout>


    </android.support.constraint.ConstraintLayout>
</FrameLayout>

您正在尝试打开同一个片段。当前片段应该被关闭。 bottomSheetFragment.dismiss(); 然后 bottomSheetFragment.show(getSupportFragmentManager(), tag)

你可以尝试给片段一个不同的标签。

public void showBottomSheetDialogFragment() {
       bottomSheetFragment = new BottomSheetFragment();
       bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}

编辑:

此错误发生在已在布局中定义的嵌套片段上,请尝试从 XML 布局中删除您的片段并将其替换为 FrameLayout,然后在代码中动态实例化您的片段。

您在 YourFragment 的 XML 文件中调用片段标记,基本上,将 bottom sheet 添加到片段 的方法是错误的。以下是您可以做到的方式:

点击按钮调用此方法:

callToGuideShareDialog();

给底部充气Sheet在这个方法里面查看

private void callToGuideShareDialog() {
final View viewBottom = DataBindingUtil.inflate(getLayoutInflater(), R.layout.------, null, false).getRoot();
            ImageView imageView = viewBottom.findViewById(R.id.img_size_guide_share);
            TextView textViewMsg = viewBottom.findViewById(R.id.----);
            viewBottom.findViewById(R.id.-----).setOnClickListener(this);
            TextView textViewMeasurement = viewBottom.findViewById(R.id.----);

        sheetDialog = new BottomSheetDialog(context);
        sheetDialog.setContentView(viewBottom);

        viewBottom.post(new Runnable() {
            @Override
            public void run() {
                BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) viewBottom.getParent());
                mBehavior.setPeekHeight(viewBottom.getHeight());
            }
        });

        if (sheetDialog != null)
            sheetDialog.show();
}