Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager #39

Open
XiongKe94 opened this issue Mar 13, 2023 · 4 comments

Comments

@XiongKe94
Copy link

Exception java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.
at androidx.fragment.app.Fragment.getLayoutInflater (Fragment.java:1673)
at androidx.fragment.app.Fragment.onGetLayoutInflater (Fragment.java:1622)
at androidx.fragment.app.Fragment.performGetLayoutInflater (Fragment.java:1654)
at androidx.fragment.app.Fragment.getLayoutInflater (Fragment.java:1639)
at com.hi.dhl.binding.viewbind.FragmentViewBinding.getValue (FragmentViewBinding.kt:51)
at com.aly.ticktube.ui.fragment.VideoFragment. (VideoFragment.kt:39)
at com.aly.ticktube.ui.fragment.VideoFragment.access$getMFragment (VideoFragment.kt:37)
at com.aly.ticktube.ui.fragment.VideoFragment$initView$6$1.onAnimationEnd (VideoFragment.kt:123)
at android.animation.AnimatorSet.onChildAnimatorEnded (AnimatorSet.java:829)
at android.animation.AnimatorSet.-wrap1 (AnimatorSet.java)
at android.animation.AnimatorSet$AnimatorSetListener.onAnimationEnd (AnimatorSet.java:784)
at android.animation.ValueAnimator.endAnimation (ValueAnimator.java:1153)
at android.animation.ValueAnimator.doAnimationFrame (ValueAnimator.java:1313)
at android.animation.AnimationHandler.doAnimationFrame (AnimationHandler.java:146)
at android.animation.AnimationHandler.-wrap2 (AnimationHandler.java)
at android.animation.AnimationHandler$1.doFrame (AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run (Choreographer.java:928)
at android.view.Choreographer.doCallbacks (Choreographer.java:705)
at android.view.Choreographer.doFrame (Choreographer.java:637)
at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:916)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6816)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1565)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1453)

@XiongKe94
Copy link
Author

FragmentViewBinding 里面代码为
`class FragmentViewBinding(
classes: Class,
fragment: Fragment
) : FragmentDelegate(fragment) {

private val layoutInflater = classes.inflateMethod()
private val bindView = classes.bindMethod()

override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
    return viewBinding?.run {
        return this

    } ?: let {

        try {
            /**
             * 检查目的,是为了防止在 onCreateView() or after onDestroyView() 使用 binding。
             * 另外在销毁之后,如果再次使用,由于 delegate property 会被再次初始化出现的异常
             *
             * 捕获这个异常的原因,是为了兼容之前的版本,防止因为升级,造成崩溃
             */
            check(thisRef.viewLifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
                "cannot use binding in before onCreateView() or after onDestroyView() from 1.1.4. about [issue](https://github.com/hi-dhl/Binding/issues/31#issuecomment-1109733307)"
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }


        val bind: T
        if (thisRef.view == null) {
            // 这里为了兼容在 navigation 中使用 Fragment
            bind = layoutInflater.invoke(null, thisRef.layoutInflater) as T
        } else {
            bind = bindView.invoke(null, thisRef.view) as T

        }

        return bind.apply { viewBinding = this }
    }
}

}`

@hi-dhl
Copy link
Owner

hi-dhl commented Mar 16, 2023

收到,能贴一下,使用这部分的代码吗

@XiongKe94
Copy link
Author

你说的是调用的时候吗? 就是正常调用呀

@XiongKe94
Copy link
Author

手机多次运行也不会报错,这个错误是在GP后台看到的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants