迁移到AndroidX后出现SingleLiveEvent问题
作者:互联网
使用Android studio 3.2.1迁移到AndroidX后,由于此错误,我无法运行应用程序:
SingleLiveEvent.java:29: error: name clash: observe(LifecycleOwner,Observer<T#1>) in SingleLiveEvent and observe(LifecycleOwner,Observer<? super T#2>) in LiveData have the same erasure, yet neither overrides the other
public void observe(LifecycleOwner owner, final Observer<T> observer) {
^
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SingleLiveEvent
T#2 extends Object declared in class LiveData
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
我用谷歌搜索,找到了解决其他问题的方法,但如何优雅地解决这个问题呢?我不想在我使用单个直播活动的每个地方重构整个应用程序.
解决方法:
我发现了这个问题.
LiveData提供了这个:
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer)
而SingleLiveEvent提供:
public void observe(LifecycleOwner owner, final Observer<T> observer)
解决方案是在SingleLiveEvent中更改方法签名以匹配LiveData
标签:android,android-studio,android-livedata 来源: https://codeday.me/bug/20190727/1549032.html