2

我无法为这个抽象类创建辅助构造函数。

@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
4

3 回答 3

9

您可以像这样调用辅助构造函数 -

@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) }
于 2019-01-08T09:12:01.700 回答
3

当您创建辅助构造函数并且类具有非空构造函数时,您应该传递构造函数具有的所有参数,例如:

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) { ... }
于 2019-01-08T08:17:32.017 回答
0

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

constructor(application: Application): this(application)

相当于java:

public MyClass(Application application){
    this(application)
}

这表示您正在递归地调用您的构造函数。


正如Alok Mishra 的回答中所述,您应该改为调用超级构造函数。

constructor(application: Application): super(application)

这实际上相当于您的主构造函数:

BaseAndroidViewModel(application: Application): AndroidViewModel(application)

您可以声明这只是关键字constructorBaseAndroidViewModel替换superAndroidViewModel

于 2019-01-08T10:45:06.033 回答