构造函数注入不适用于Kotlin类

时间:2019-01-23 09:54:35

标签: android kotlin dagger-2

我已经将我的Dagger模块,组件和类转换为Kotlin。在Model类中使用构造函数注入时遇到以下错误:

java.lang.IllegalStateException: dataManager.appSettings must not be null

这是我的模块类:

@Module
class BaseModule {
    @Provides
    fun provideBaseViewModel(application: Application, dataManager: DataManager, serviceConnector: ServiceConnector, compositeDisposable: CompositeDisposable): BaseModel {
        return BaseModel(application, dataManager, serviceConnector, compositeDisposable)
    }
}

这是我的具有构造函数注入的Model类:

class BaseModel @Inject constructor(application: Application, dataManager: DataManager,compositeDisposable: CompositeDisposable) : BaseViewModel(application, dataManager, compositeDisposable) {

    val appSettings: AppSettings
        get() = dataManager.appSettings
}

这是组件类:

@Component(modules = [BaseModule::class])
interface BaseComponent {
    fun inject(activity: BaseActivity)
}

1 个答案:

答案 0 :(得分:0)

dataManager.appSettings是否可以为空?

如果是,您可以将AppSettings设置为可空,如下所示:

val appSettings: AppSettings?