java.lang.IllegalStateException: 未添加视图

java.lang.IllegalStateException: view not added

我收到以下异常。不明白为什么。试图重现问题 too.But 失败。有人可以帮助我理解为什么我会遇到这个问题以及如何解决它。它也不是一直都在发生。

Fatal Exception: java.lang.IllegalStateException: view not added
       at com.android.internal.policy.PhoneWindow.getViewRootImpl(PhoneWindow.java:1851)
       at com.android.internal.policy.PhoneWindow.setLocalFocus(PhoneWindow.java:1835)
       at com.qikwork.worker.views.fragment.MyBottomSheetFragment$onCreateView.onShow(MyBottomSheetFragment.java:53)
       at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1465)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:233)
       at android.app.ActivityThread.main(ActivityThread.java:7212)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

代码如下:

class MyBottomSheetFragment() : BottomSheetDialogFragment(), ClockStatusBroadCastListener { ...

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    binding = DataBindingUtil.inflate(
        inflater,
        R.layout.my_bottomsheet,
        container,
        false
    )

    dialog!!.setOnShowListener { dia ->
        val bottomSheetDialog = dia as BottomSheetDialog
        val bottomSheetInternal: FrameLayout =
            bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
        val bottomSheetBehaviour = BottomSheetBehavior.from(bottomSheetInternal)
        bottomSheetBehaviour.state = BottomSheetBehavior.STATE_EXPANDED
        bottomSheetInternal.layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
        bottomSheetDialog.window?.setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN or
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
        )
        bottomSheetDialog.window?.setLocalFocus(true, true)
    }
    return binding.root
}.....

第 53 行指向 R.layout.my_bottomsheet

使用 setLocalFocus(true, true) 导致 issue.Removing 有效。