底片碎片第二次调用时导致错误

时间:2019-04-05 11:35:21

标签: java android android-fragments bottom-sheet

我有一个类BottomSheetFragment extends BottomSheetDialogFragment,其中包含子片段,我从一个片段中调用它。 问题是,当我第二次调用它时,我的应用程序因Binary XML file line #69: Duplicate id 0x7f090071, tag null, or parent id 0xffffffff with another fragment错误而崩溃。

底页类:

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类在按钮单击的另一个片段中这样调用:

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中的方法,该方法显示了底部表格:

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

我假定由于某种原因第二次致电CreateNotesFragment时,BottomSheetFragment不再存在,但是我该如何解决?

谢谢。


更新
这是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" />

以及用于确保没有ID重复的整个XML:

<?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>

2 个答案:

答案 0 :(得分:1)

您正在尝试打开相同的片段。当前片段应被消除。 bottomSheetFragment.dismiss();然后bottomSheetFragment.show(getSupportFragmentManager(), tag)

您可以尝试给片段加上其他标签。

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

编辑:

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

答案 1 :(得分:0)

您是在YourFragment的XML文件中调用片段标记,基本上,将底部工作表添加到片段是错误的方法。这是您可以执行的方法:

在botton上调用此方法,请单击:

callToGuideShareDialog();

在此方法内为底部的工作表视图填充

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();
}