作者:BaoBao佳佳佳 | 来源:互联网 | 2022-12-28 21:08
1> azizbekian..:
发生这种情况,因为您已声明,DataModule
需要一个应用程序对象的实例才能构造,但您尚未指定如何创建该模块.如果带@Module
注释的类在其构造函数中没有任何参数(即具有默认构造函数),则dagger将创建该模块.否则,您必须指定如何手动创建模块.
将最顶层(应用程序)组件更改为以下内容:
@Component(modules = [...])
interface AppComponent {
@Component.Builder
interface Builder {
// @BindsInstance will make `application` to be accessible in the graph
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}
现在更改DataModule
为要求应用程序对象的实例:
@Module
class DataModule {
@Provides
fun provideDatabase(application: Application): Database {
return ...
}
}