[英]Bottom Sheet Fragment causes error when called the second time
I have a class BottomSheetFragment extends BottomSheetDialogFragment
, which contains sub-fragments, and I call it from a fragment. 我有一个类
BottomSheetFragment extends BottomSheetDialogFragment
,它包含子片段,我从片段中调用它。 The problem is that when I call it the second time, my app crashes with Binary XML file line #69: Duplicate id 0x7f090071, tag null, or parent id 0xffffffff with another fragment
error. 问题是,当我第二次调用它时,我的应用程序崩溃了
Binary XML file line #69: Duplicate id 0x7f090071, tag null, or parent id 0xffffffff with another fragment
错误。
Bottom 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;
}
}
In its XML it contains a sub-fragment public class CreateNotesFragment extends android.support.v4.app.Fragment
在它的XML中它包含一个子片段
public class CreateNotesFragment extends android.support.v4.app.Fragment
The BottomSheetFragment
class is called in another fragment on button click like this: 在按钮点击的另一个片段中调用
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;
}
And here is the method in MainActivity
which shows the bottom sheet: 以下是
MainActivity
中显示底部工作表的方法:
public void showBottomSheetDialogFragment() {
bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
I assume that the CreateNotesFragment
no longer exists when I call the BottomSheetFragment
for the second time for some reason, but how can I fix it? 我假设当我因某种原因第二次调用
BottomSheetFragment
时, CreateNotesFragment
不再存在,但我该如何解决呢?
Thank you. 谢谢。
fragment_bottom_sheet.xml
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" />
And the whole XML to make you sure that there is no ID duplicates: 整个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>
I also noticed that if I remove this fragment from XML, I can open BottomSheetFragment
without any crashes, so the problem must be it the sub-fragment. 我还注意到,如果我从XML中删除此片段,我可以打开
BottomSheetFragment
而不会发生任何崩溃,因此问题必须是子片段。
BottomSheetFragment
now looks like this:
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>
You are trying to open the same fragment. 您正在尝试打开相同的片段。 The current fragment should be dismissed.
目前的片段应该被驳回。
bottomSheetFragment.dismiss();
then bottomSheetFragment.show(getSupportFragmentManager(), tag)
然后是
bottomSheetFragment.show(getSupportFragmentManager(), tag)
And you can try to give a different tag to fragment. 你可以尝试给片段添加不同的标签。
public void showBottomSheetDialogFragment() {
bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
Edit : 编辑:
This error occurs on Nested Fragments which has been defined in layout, try to remove your Fragment from XML Layout and replace it with a FrameLayout then instantiate your Fragment dynamically in code. 在布局中定义的嵌套片段上发生此错误,尝试从XML布局中删除片段并将其替换为FrameLayout,然后在代码中动态实例化片段。
You are calling fragment tag inside XML file of YourFragment, basically, it's a wrong way to add bottom sheet to fragment . 您正在调用YourFragment的XML文件中的片段标记,基本上,这是将底部片材添加到片段的错误方法。 Here is the way you can do it:
以下是您可以这样做的方法:
Call this method on botton Click: 在botton上调用此方法单击:
callToGuideShareDialog();
Inflate the bottom Sheet View inside this method 在此方法中为底部工作表视图充气
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.