使用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.
我搜索了它,找到了其他解决方案,但是如何优雅地解决呢?我不想在使用单个直播活动的每个地方重构整个应用程序。
答案 0 :(得分:11)
我发现了问题。 LiveData提供此功能:
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer)
而SingleLiveEvent提供:
public void observe(LifecycleOwner owner, final Observer<T> observer)
解决方案是更改SingleLiveEvent中的方法签名以匹配LiveData
答案 1 :(得分:0)
我正在Kotlin中寻找此解决方案:
override fun observe(@NonNull owner: LifecycleOwner, @NonNull observer:Observer<in T>)