当我尝试使用dagger为我的房间数据库提供Dao时,DaggerAppComponent首次生成,但是当我在Application上实现HasActivityInjector时,它突然变得无法解析。这是代码:
应用程序:
class StudentApplication : Application(), HasActivityInjector{
@Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity>
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder().appModule(AppModule(applicationContext)).build()
appComponent.inject(this)
}
override fun activityInjector(): DispatchingAndroidInjector<Activity> = activityInjector }
这是AppModule
@Module
class AppModule (private val context: Context){
@Provides
fun provideContext() = context
@Provides @Singleton fun provideStudentDatabase(context: Context): StudentDatabase = Room.databaseBuilder(context,
StudentDatabase::class.java, "studentdata.db")
.build()
@Provides @Singleton fun provideStudentDao(studentDatabase: StudentDatabase) = studentDatabase.studentDao()
}
这是AppComponent:
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
fun inject(application: StudentApplication)
}
在此先感谢您,我们将不胜感激。