如果没有Android Kotlin mvvm中的主要构造函数,则无法进行超级类型初始化

时间:2019-07-18 10:33:46

标签: android kotlin mvvm

我要做什么:

我正在尝试将viewModel链接到活动中,并在两者之间使用工厂

MainActivity.kt

KtMainActivityViewModelFactory    ktMainActivityViewModel = ViewModelProviders.of(this,ktMainActivityViewModelFactory).get(KtMainActivityViewModel::class.java)

KtMainActivityViewModelFactory.kt

class KtMainActivityViewModelFactory : ViewModelProvider.Factory {


    private val movieRepository: MovieRepository
    private val database: MoviesAppDatabase
    private val dataservice: MovieDataService

    @Inject
    constructor(movieRepository: MovieRepository, database: MoviesAppDatabase, dataservice: MovieDataService) {
        this.movieRepository = movieRepository
        this.database = database
        this.dataservice = dataservice
    }


    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return KtMainActivityViewModelFactory(movieRepository, database, dataservice) as T
    }


}

KtMainActivityViewModel.kt

class KtMainActivityViewModel : ViewModel() {


    private val movieRepository: MovieRepository
    private val database: MoviesAppDatabase
    private val dataservice: MovieDataService


    constructor(movieRepository: MovieRepository, database: MoviesAppDatabase, dataservice: MovieDataService) : super() {
        this.movieRepository = movieRepository
        this.database = database
        this.dataservice = dataservice
    }


}

错误: enter image description here

3 个答案:

答案 0 :(得分:1)

class KtMainActivityViewModel : ViewModel() {


private lateinit var movieRepository: MovieRepository
private lateinit var database: MoviesAppDatabase
private lateinit var dataservice: MovieDataService


constructor(movieRepository: MovieRepository, database: MoviesAppDatabase, dataservice: MovieDataService) : super() {
    this.movieRepository = movieRepository
    this.database = database
    this.dataservice = dataservice
    }
 }

您应添加主要构造函数,并将super()替换为this()

class KtMainActivityViewModel(): ViewModel() {

答案 1 :(得分:1)

尝试将其更改为KtMainActivityViewModel : ViewModel

答案 2 :(得分:0)

由于只有一个构造函数,因此应将其设为主要构造函数:

class KtMainActivityViewModel(private val movieRepository: MovieRepository, private val database: MoviesAppDatabase, private val dataservice: MovieDataService) : ViewModel() { ... }

作为一项额外的好处,这种方式还需要每个属性提及一次,而不是4次。

请注意,您可以在正文中声明private val movieRepository: MovieRepository

class KtMainActivityViewModel(movieRepository: MovieRepository, database: MoviesAppDatabase, dataservice: MovieDataService) : ViewModel() { 
    private val _movieRepository: MovieRepository = movieRepository
    ...
}

但没有理由。

当然这也适用于KtMainActivityViewModelFactory

class KtMainActivityViewModelFactory @Inject constructor(private val movieRepository: MovieRepository, private val database: MoviesAppDatabase, private val dataservice: MovieDataService) : ViewModelProvider.Factory {

    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return KtMainActivityViewModelFactory(movieRepository, database, dataservice) as T
    }

}