3

我收到以下异常。无法理解为什么。也尝试重现该问题。但无法成功。有人可以帮我理解为什么我会遇到这个问题以及如何解决它。它也不会一直发生。

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$1.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)

代码如下:

类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

4

1 回答 1

0

使用setLocalFocus(true, true)导致问题。删除有效。

于 2021-06-22T06:38:15.387 回答