Android 为什么我从AutoclearedValue获得此异常?

Android 为什么我从AutoclearedValue获得此异常?,android,kotlin,Android,Kotlin,我在android中使用AutoclearedValue,在运行android 4.4.4的旧平板电脑中,我总是遇到这样一个异常:当AutoclearedValue类中可能无法使用AutoclearedValue get时,不应调用AutoclearedValue get 我不明白为什么,因为在较新的设备中,我没有得到这个例外 场景如下:我在recyclerview中单击一个项目,然后打开另一个片段。在这个片段中,我实现了简单的scrollview监听器,它工作得非常好。当我单击back按钮,导

我在android中使用AutoclearedValue,在运行android 4.4.4的旧平板电脑中,我总是遇到这样一个异常:当AutoclearedValue类中可能无法使用AutoclearedValue get时,不应调用AutoclearedValue get

我不明白为什么,因为在较新的设备中,我没有得到这个例外

场景如下:我在recyclerview中单击一个项目,然后打开另一个片段。在这个片段中,我实现了简单的scrollview监听器,它工作得非常好。当我单击back按钮,导航回上一个片段时,我得到了异常

我的AutoClearedValue类:

class AutoClearedValue<T : Any>(val fragment: Fragment) : ReadWriteProperty<Fragment, T> {
    private var _value: T? = null

    init {
        fragment.lifecycle.addObserver(object: DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewLifecycleOwner ->
                    viewLifecycleOwner?.lifecycle?.addObserver(object: DefaultLifecycleObserver {
                        override fun onDestroy(owner: LifecycleOwner) {
                            _value = null
                        }
                    })
                }
            }
        })
    }

    override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
        return _value ?: throw IllegalStateException(
            "should never call auto-cleared-value get when it might not be available"
        )
    }

    override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) {
        _value = value
    }
}

/**
 * Creates an [AutoClearedValue] associated with this fragment.
 */
fun <T : Any> Fragment.autoCleared() = AutoClearedValue<T>(this)
class AutoClearedValue(val fragment:fragment):读写属性{
私有变量_值:T?=null
初始化{
fragment.lifecycle.addObserver(对象:DefaultLifecycleObserver{
重写创建时的乐趣(所有者:LifecycleOwner){
fragment.viewLifecycleOwnerLiveData.observe(fragment){viewLifecycleOwner->
viewLifecycleOwner?.lifecycle?.addObserver(对象:DefaultLifecycleObserver{
覆盖趣味onDestroy(所有者:LifecycleOwner){
_值=空
}
})
}
}
})
}
重写fun getValue(thisRef:Fragment,property:KProperty):T{
返回值?:抛出IllegalStateException(
“当自动清除值get可能不可用时,不应调用该值”
)
}
重写fun setValue(thisRef:Fragment,property:KProperty,value:T){
_价值=价值
}
}
/**
*创建与此片段关联的[AutoClearedValue]。
*/
fun Fragment.autoCleared()=AutoClearedValue(此)
还有我的听众,例外来自哪里:

    private fun initListeners() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            binding.freightTaskRowDetailScrollView.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
                if (binding.showFab == true) {
                    if (scrollY <= oldScrollY) {
                        if (!binding.expandableFab.isShown)
                            binding.expandableFab.showHide()
                    } else {
                        if (binding.expandableFab.isShown)
                            binding.expandableFab.showHide()
                    }
                }
            }
        } else {
            binding.freightTaskRowDetailScrollView.viewTreeObserver
                .addOnScrollChangedListener {
                    if (binding.showFab == true) { [this line]
                        if (!binding.freightTaskRowDetailScrollView.canScrollVertically(1)) {
                            if (binding.expandableFab.isShown)
                                binding.expandableFab.showHide()
                        }
                        if (!binding.freightTaskRowDetailScrollView.canScrollVertically(-1)) {
                            if (!binding.expandableFab.isShown)
                                binding.expandableFab.showHide()
                        }
                    }
                }
        }
    }

private-fun initListeners(){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
binding.freightTaskRowDetailScrollView.setOnScrollChangeListener{{{uu,{uu,scrollY,},oldScrollY->
if(binding.showFab==true){

如果(滚动更新:

AutoClearedValue在ViewBinding方面有一个bug,您应该使用此链接中提供的类进行ViewBinding

旧答案

您拥有该类的旧版本

请在此处检查新的实现: