3

I created a BottomSheetDialogUtil class which extends BottomSheetDialogFragment. This class can be used as long as you pass in a view which you want to be displayed in the bottom sheet dialog.

Showing the BottomSheetDialogFragment works when you show it the first time, but when you try to show it for a second time it crashes

This is where I call the BottomSheetDialogUtil class

private void configureBudgetPeriodSpinner() {
    OrderPaymentCreditCardBudgetOptionsBinding binding = OrderPaymentCreditCardBudgetOptionsBinding.inflate(getLayoutInflater());

    BottomSheetDialogUtil bottomSheetDialog = BottomSheetDialogUtil.newInstance(binding.getRoot());

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.credit_card_budget_period, R.layout.spinner_budget_option);

    binding.listView.setAdapter(adapter);

    binding.listView.setOnItemClickListener((aAdapterView, aView, aI, aL) -> {
      String string = binding.listView.getItemAtPosition(aI).toString();

      mBinding.formInclude.budgetOptionsTextInput.setText(string);
      mViewModel.setBudgetPeriod(CreditCardUtil.getBudgetPeriodInt(string));
      bottomSheetDialog.dismiss();
    });

    mBinding.formInclude.budgetOptionsTextInput.setOnClickListener(aView -> bottomSheetDialog.show(getFragmentManager(), "Bottom Sheet Dialog Fragment"));
  }

This is the Util class

public class BottomSheetDialogUtil extends BottomSheetDialogFragment {

  private static View mView;

  public static BottomSheetDialogUtil newInstance(View aView) {
    mView = aView;
    return new BottomSheetDialogUtil();
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public void setupDialog(Dialog dialog, int style) {
    dialog.setContentView(mView);
    ((View) mView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }
}

This is the error I'm getting

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bidorbuy.app, PID: 21202
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5252)
        at android.view.ViewGroup.addView(ViewGroup.java:5083)
        at android.view.ViewGroup.addView(ViewGroup.java:5023)
        at android.view.ViewGroup.addView(ViewGroup.java:4996)
        at com.google.android.material.bottomsheet.BottomSheetDialog.wrapInBottomSheet(BottomSheetDialog.java:137)
        at com.google.android.material.bottomsheet.BottomSheetDialog.setContentView(BottomSheetDialog.java:88)
        at com.bidorbuy.app.v3.util.BottomSheetDialogUtil.setupDialog(BottomSheetDialogUtil.java:32)
        at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:333)
        at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6986)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Community
  • 1
  • 1
Lebogang Nkosi
  • 327
  • 1
  • 4
  • 14
  • Possible duplicate of [The specified child already has a parent. You must call removeView() on the child's parent first (Android)](https://stackoverflow.com/questions/28071349/the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil) – a_local_nobody Sep 19 '19 at 06:04
  • The error is the same, but the context isn't. Did you find a solution for the error using a bottom sheet? – padmalcom Feb 14 '23 at 14:06

0 Answers0