本文介紹了如何修復dagger2庫中沒有@Provides注解的方法";錯誤時無法提供的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
代碼
我剛剛開始學習Dagger2,在實現以下模塊時遇到了問題。
AppComponent:
@Singleton
@Component(modules = [AndroidInjectionModule::class, AppModule::class, ActivityBuilder::class])
interface AppComponent {
fun inject(app: App)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}
App:
class App : Application(), HasAndroidInjector {
@Inject
lateinit var androidInjector: DispatchingAndroidInjector<Any>
override fun androidInjector() = androidInjector
override fun onCreate() {
super.onCreate()
initDagger()
}
private fun initDagger() {
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this)
}
}
AppModule:
@Module
class AppModule {
@Provides
@Singleton
fun provideContext(application: Application): Context {
return application
}
@Provides
@ApiInfo
fun provideApiKey(): String {
return "mApiKey"
}
@Provides
@Singleton
fun providePreferencesHelper(appPreferencesHelper: AppPreferencesHelper): PreferencesHelper {
return appPreferencesHelper
}
@Provides
fun provideSchedulerProvider(): SchedulerProvider {
return AppSchedulerProvider()
}
}
數據管理器
interface DataManager : PreferencesHelper {
enum class LoggedInMode constructor(type: Int) {
LOGGED_IN_MODE_LOGGED_OUT(0),
LOGGED_IN_MODE_GOOGLE(1),
LOGGED_IN_MODE_FB(2),
LOGGED_IN_MODE_SERVER(3);
private var mType: Int = type
fun getType(): Int {
return mType
}
}
}
ActivityBuilder:
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector
abstract fun bindSplashActivity(): SplashActivity
}
ViewModelProviderFactory:
@Singleton
class ViewModelProviderFactory @Inject
constructor(
private val dataManager: DataManager,
private val schedulerProvider: SchedulerProvider
) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SplashViewModel::class.java)) {
return SplashViewModel(dataManager, schedulerProvider) as T
}
throw IllegalArgumentException("Unknown ViewModel class: " + modelClass.name)
}
}
錯誤
我從此實現中收到錯誤是…
錯誤:如果沒有@Provides注釋的方法,則無法提供[Dagger/MissingBinding]com.example.Package.data.DataManager。
public abstract interface AppComponent {
^
com.example.package.data.DataManager is injected at
com.example.package.ViewModelProviderFactory(dataManager, …)
com.example.package.ViewModelProviderFactory is injected at
com.example.package.ui.splash.SplashActivity.factory
com.example.package.ui.splash.SplashActivity is injected at
dagger.android.AndroidInjector.inject(T) [com.example.package.di.component.AppComponent ? com.example.package.di.builder.ActivityBuilder_BindSplashActivity.SplashActivitySubcomponent]
推薦答案
匕首無法注入接口。你也做不到。
我看到DataManager是一個接口。如果您想注入它的實現,如果我們假設該實現名為:DataManagerImpl,則需要使用以下代碼。
@Provides
fun provideDataManager (): DataManager {
return DataManagerImpl()
}
否則您的代碼就沒有意義了。我的意思是:
@Singleton
class ViewModelProviderFactory @Inject
constructor(
private val dataManager: DataManager, <<<<------ HERE?!
private val schedulerProvider: SchedulerProvider
) : ViewModelProvider.NewInstanceFactory() {
這篇關于如何修復dagger2庫中沒有@Provides注解的方法";錯誤時無法提供的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,