BottomSheetDialogFragment-指定的子代已经有一个父代。您必须先在孩子的父母上调用removeView()

时间:2019-09-19 05:22:32

标签: android bottom-sheet

我创建了一个扩展BottomSheetDialogUtil class的{​​{1}}。只要在要显示在底部工作表对话框中的视图中传递,就可以使用此类。

第一次显示BottomSheetDialogFragment时可以正常显示,但是第二次尝试显示时会崩溃

这是我称为BottomSheetDialogUtil类的地方

BottomSheetDialogFragment

这是Util类

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"));
  }

这是我遇到的错误

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

0 个答案:

没有答案