gpt4 book ai didi

android - LifecycleObserver 使用较新 API 的方法产生异常

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:27 24 4
gpt4 key购买 nike

我的 ViewModel 类实现了 LifecycleObserver。当我调用 fragment.lifecycle.addObserver(this) 时,它会产生异常。

Caused by: java.lang.IllegalArgumentException: The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.

奇怪,本来还好好的,前不久就出现了这个异常。我发现,audioFocusRequest 是导致此错误的原因。

private val audioFocusRequest by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(this)
.build() else throw RuntimeException("Can't be done for Android API lower than 26")
}

有人知道怎么解决吗?

UPD

尝试使用 annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version",但出现编译错误: enter image description here(决定粘贴截图,因为整个日志很大)

UPD 2
最后,我决定删除 audioFocusRequest 字段并使用旧的弃用方法 - requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) 而不是推荐的 requestAudioFocus (@NonNull AudioFocusRequest focusRequest)

它帮助我使代码再次工作,所以它可以成为解决方案。但我没有找到答案——为什么会出现这个问题。这很奇怪,因为以前的代码可以正常工作。

所以问题已经解决,但问题仍然没有答案

最佳答案

尝试使用kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"

关于android - LifecycleObserver 使用较新 API 的方法产生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54555764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com