kotlin:1.2.71 andorid工作室:3.2.1 gradle插件:3.2.0 gradle:4.6
我收到“意外覆盖”错误(下面的代码示例),如果您发现任何错误使用,请告诉我,不胜感激。
abstract class ResourceLiveData<T: Resource<*>> : LiveData<T>(){
override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
do something here ...
super.observe(owner, observer)
}
}
演示隐含代码:
// Inherited platform declarations clash: The following declarations have the same JVM signature (observe(Landroidx/lifecycle/LifecycleOwner;Landroidx/lifecycle/Observer;)V):
// fun observe(owner: LifecycleOwner, observer: Observer<Resource<String>>): Unit defined in com.demo.live.StringResourceLiveData
// fun observe(p0: LifecycleOwner, p1: Observer<in Resource<String>!>): Unit defined in com.demo.live.StringResourceLiveData
class StringResourceLiveData : ResourceLiveData<Resource<String>>()
ResourceLiveData
位于第三方库中,而库依赖项android.arch.lifecycle
位于。
我的应用程序正在迁移到androidx,因此,我在gradle.properties中添加了android.useAndroidX=true
和android.enableJetifier=true
。
同步后,ResourceLiveData.class
导入已更改为androidx,但是这里的问题是,ResourceLiveData
覆盖方法不包含变量in
,但是androidx.lifecycle.LiveData
需要它
答案 0 :(得分:2)
您的代码有很多问题。
ResourceLiveData
,则必须为open
。 ()
这是更正的代码
open class ResourceLiveData<T: Resource<*>> : LiveData<T>(){
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
do something here ...
super.observe(owner, observer)
}
}
class StringResourceLiveData : ResourceLiveData<Resource<String>>()