簡體   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