添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
@SuppressLint("StaticFieldLeak")
abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: CreditsGetByIdUseCase,
        private val videosGetByIdUseCase: VideosGetByIdUseCase,
        private val reviewGetByIdUseCase: ReviewGetByIdUseCase,
        private val addToFavoritesUseCase: AddToFavoritesUseCase,
        private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase
) : AndroidViewModel(application) {
constructor(application: Application) : this(application) // There's a cycle in the delegation calls chain error
    
android
kotlin
constructor
class-constructors
Sergejs Bogdanovs
Sergejs Bogdanovs
发布于 2019-01-08
3 个回答
Alok Mishra
Alok Mishra
发布于 2019-01-08
已采纳
0 人赞同

你可以像这样调用二级构造函数--

@SuppressLint("StaticFieldLeak") abstract class BaseAndroidViewModel : AndroidViewModel{
constructor(application: Application,
            creditsGetByIdUseCase: CreditsGetByIdUseCase,
            videosGetByIdUseCase: VideosGetByIdUseCase,
            reviewGetByIdUseCase: ReviewGetByIdUseCase,
            addToFavoritesUseCase: AddToFavoritesUseCase,
            getFavoriteByIdUseCase: GetFavoriteByIdUseCase) : super(application)
constructor(application: Application) : super(application) }
    
Sergio
Sergio
发布于 2019-01-08
0 人赞同

当你创建一个次级构造函数,并且一个类有非空的初级构造函数,你应该传递所有的参数初级构造函数有,例如。

abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: String,
        private val videosGetByIdUseCase: String
) : AndroidViewModel(application) {
    constructor(application: Application) : this(application, 
            "creditsGetByIdUseCase", "videosGetByIdUseCase") // here we pass other necessary parameters

在你的情况下,它可能是以下情况。

@SuppressLint("StaticFieldLeak")
abstract class BaseAndroidViewModel(
    application: Application,
    private val creditsGetByIdUseCase: CreditsGetByIdUseCase?,
    private val videosGetByIdUseCase: VideosGetByIdUseCase?,
    private val reviewGetByIdUseCase: ReviewGetByIdUseCase?,
    private val addToFavoritesUseCase: AddToFavoritesUseCase?,
    private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase?
) : AndroidViewModel(application) {
    constructor(application: Application) : this(application, null, null, null, null, null)

或者你可以创建初级带默认参数的构造函数。

abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: CreditsGetByIdUseCase? = null,
        private val videosGetByIdUseCase: VideosGetByIdUseCase? = null,
        private val reviewGetByIdUseCase: ReviewGetByIdUseCase? = null ,
        private val addToFavoritesUseCase: AddToFavoritesUseCase? = null,
        private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase? = null
    ) : AndroidViewModel(application) { ... }
    
crgarridos
crgarridos
发布于 2019-01-08
0 人赞同

你得到cycle in the delegation calls chain error的原因,是因为

constructor(application: Application): this(application)

是等同于java的。

public MyClass(Application application){
    this(application)

这说明你正在递归地调用你的构造函数。

正如在阿洛克-米什拉的回答,你应该调用超级构造函数。

constructor(application: Application): super(application)

这实际上相当于你的主构造函数。