这是我的应用程序组件类
@Singleton
@Component(dependencies = {}, modules = {AppModule.class,RoomModule.class, NetworkModule.class})
public interface AppComponent {
void inject(CSATApplication applicationController);
void inject(AverageScoreActivity averageScoreActivity);
CsatDao csaatdao();
CSATDatabase csatDatabase();
RemoteRepositoryImpl getRemoteRepo();
LocalRepositoryImpl getLocalRepo();
Application application();
BaseUrlHolder provideBaseUrlHolder();
Retrofit getRetrofit();
CompositeDisposable getCompositeDisposable();
CompositeDisposable getVMCompositeDisposable();
}
@模块 公共类RoomModule {
private CSATDatabase CSATDatabase;
public RoomModule(Application mApplication) {
CSATDatabase = Room.databaseBuilder(mApplication, CSATDatabase.class, AppConstants.DATABASE_NAME).build();
}
@Singleton
@Provides
CSATDatabase providesRoomDatabase() {
return CSATDatabase;
}
@Singleton
@Provides
CsatDao providesCsatDao(CSATDatabase CSATDatabase) {
return CSATDatabase.getCsatDao();
}
@Singleton
@Provides
public RemoteRepository getRemoteRepo(NetworkService networkService){
return new RemoteRepositoryImpl(networkService);
}
@Singleton
@Provides
public LocalRepository getLocalRepo(CsatDao csatDao, Executor exec){
return new LocalRepositoryImpl(csatDao, exec);
}
@Provides
@Named("activity")
public CompositeDisposable getCompositeDisposable(){
return new CompositeDisposable();
}
@Provides
@Named("vm")
public CompositeDisposable getVMCompositeDisposable(){
return new CompositeDisposable();
}
注入活动
@Inject @field:Named("activity")
lateinit var compositeDisposable: CompositeDisposable
@Inject
lateinit var averageViewModelFactory: AverageViewModelFactory
@Inject
lateinit var averageViewModel: AverageViewModel
注入View模型工厂
class AverageViewModelFactory @Inject
constructor() : @JvmSuppressWildcards ViewModelProvider.Factory {
@Inject
lateinit var localRepository: LocalRepository
@Inject
lateinit var remoteRepository: RemoteRepository
@Inject @field:Named("vm")
lateinit var compositeDisposable: CompositeDisposable
@Override
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(AverageViewModel::class.java)) {
return AverageViewModel(localRepository, remoteRepository, compositeDisposable) as T
}
throw IllegalArgumentException("Wrong ViewModel class")
}
}
class AverageViewModel @Inject constructor(
val localRepository: LocalRepository,
val remoteRepository: RemoteRepository,
val compositeDisposable: CompositeDisposable
) : BaseViewModel<AverageInteractor.view>(){
Gradel
//Dependencies injection
implementation 'com.google.dagger:dagger:2.19'
implementation 'com.google.dagger:dagger-android:2.19'
kapt 'com.google.dagger:dagger-compiler:2.19'
annotationProcessor 'com.google.dagger:dagger-compiler:2.19'
kapt 'com.google.dagger:dagger-android-processor:2.19'
compileOnly 'javax.annotation:jsr250-api:1.0'
错误 错误:[Dagger / MissingBinding]如果没有@Inject构造函数或@Provides注释方法,则无法提供io.reactivex.disposables.CompositeDisposable。 公共接口AppComponent { ^ io.reactivex.disposables.CompositeDisposable在以下位置提供 com.sisindia.csat.deps.AppComponent.getCompositeDisposable() 也可以在以下位置提出要求: com.sisindia.csat.projectmodules.score.AverageViewModel(…,compositeDisposable) 以下其他入口点也依赖于此: com.sisindia.csat.deps.AppComponent.inject(com.sisindia.csat.projectmodules.score.AverageScoreActivity) com.sisindia.csat.deps.AppComponent.getVMCompositeDisposable()
答案 0 :(得分:0)
您需要在CompositeDisposable
和@Named
类中用AppComponent
来注释AverageViewModel
,像这样:
public interface AppComponent {
...
@Named("activity") CompositeDisposable getCompositeDisposable();
@Named("vm") CompositeDisposable getVMCompositeDisposable();
}
class AverageViewModel @Inject constructor(
...
@Named("vm") val compositeDisposable: CompositeDisposable
) : BaseViewModel<AverageInteractor.view>(){