底片碎片在第二次调用时导致错误 [英] Bottom Sheet Fragment causes error when called the second time
问题描述
我有一个类 BottomSheetFragment扩展了BottomSheetDialogFragment
,它包含子片段,我从一个片段中调用它.问题是,当我第二次调用它时,我的应用程序崩溃,并出现二进制XML文件第69行:重复的ID 0x7f090071,标记为null或父ID 0xffffffff并带有另一个片段
错误.
I have a class BottomSheetFragment extends BottomSheetDialogFragment
, which contains sub-fragments, and I call it from a fragment.
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.
底页类:
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中包含一个子片段公共类CreateNotesFragment扩展了android.support.v4.app.Fragment
In its XML it contains a sub-fragment public class CreateNotesFragment extends android.support.v4.app.Fragment
BottomSheetFragment
类在按钮单击的另一个片段中这样调用:
The BottomSheetFragment
class is called in another fragment on button click like this:
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
中的方法,该方法显示了底部表格:
And here is the method in MainActivity
which shows the bottom sheet:
public void showBottomSheetDialogFragment() {
bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
我假定由于某种原因第二次调用 BottomSheetFragment
时, CreateNotesFragment
不再存在,但是我该如何解决?
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?
谢谢.
更新
这是
fragment_bottom_sheet.xml
<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:
And the whole XML to make you sure that there is no ID duplicates:
<?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
现在看起来像这样:
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.
Solved
The solution is adding fragments into a fragment dynamically, not through XML. So, my
BottomSheetFragment
now looks like this:
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)
You are trying to open the same fragment. The current fragment should be dismissed. bottomSheetFragment.dismiss();
then 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());
}
此错误发生在布局中已定义的嵌套片段上,请尝试从XML布局中删除您的片段,然后将其替换为FrameLayout,然后在代码中动态实例化您的片段.
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.
这篇关于底片碎片在第二次调用时导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!