繁体   English   中英

第二次调用时,Bottom Sheet Fragment会导致错误

[英]Bottom Sheet Fragment causes error when called the second time

我有一个类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());
    }

我假设当我因某种原因第二次调用BottomSheetFragment时, CreateNotesFragment不再存在,但我该如何解决呢?

谢谢。


更新
这是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文件中的片段标记,基本上,这是将底部片材添加到片段的错误方法。 以下是您可以这样做的方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM