使用Dagger 2注入Androidx Activity

时间:2019-04-19 10:17:43

标签: android dependency-injection dagger-2 dagger

我是使用Android中的Dagger进行依赖项注入的新手

我遇到此错误Unable to start activity ComponentInfo{MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property dispatchingAndroidInjector has not been initialized

我不知道代码中的确切错误是什么,我应该更改一些内容吗?尽管在我从事的另一个项目中,除了AndroidInjection.inject(this)之外,它们具有相同的代码,并且代码运行正常

非常感谢您的帮助

这是我的文件:

AppComponent

@Singleton
@Component(
modules = [AndroidSupportInjectionModule::class,
    AppModule::class, NetworkModule::class,
    ActivityBuilder::class]
)
interface AppComponent : AndroidInjector<Application> {

@Component.Builder
interface Builder {
    @BindsInstance
    fun application(application: Application): Builder

    fun build(): AppComponent
}

}

AppModule

@Module(includes = [ViewModelModule::class])

AppModule类

@Provides
fun provideContext(application: MyApplication): Context {
    return application.applicationContext
}

@Provides
fun provideHandler(): Handler {
    return Handler()
}

@Provides
@Singleton
fun provideSharedPreferences(context: Context): SharedPreferences {
    return context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
}

@Provides
@Singleton
fun provideSharedPreferenceUtils(context: Context): SharedPreferenceUtils {
    return SharedPreferenceUtils(provideSharedPreferences(context))
}

}

ActivityBuilder

@Module
abstract class ActivityBuilder {

@ContributesAndroidInjector
abstract fun bindMainActivity(): MainActivity

}

NetworkModule

@Module(includes = [AppModule::class])
// Safe here as we are dealing with a Dagger 2 module
@Suppress("unused")
class NetworkModule {

@Provides
@Singleton
fun provideCache(context: Context): Cache {
    return Cache(context.cacheDir, CACHE_SIZE)
}

@Provides
@Singleton
fun provideOkHttpClient(cache: Cache, context: Context): OkHttpClient {
    return OkHttpClient.Builder()
            .cache(cache)
            .addInterceptor { chain ->
                var request = chain.request()
                request = if (hasNetwork(context)!!)
                    request.newBuilder().header("Cache-Control", "public, max-age=" + 5).build()
                else
                    request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build()
                chain.proceed(request)
            }
            .build()
}

/**
 * Provides the Quotes service implementation.
 * @param retrofit the Retrofit object used to instantiate the service
 * @return the Quote service implementation.
 */
@Provides
@Singleton
fun provideQuotesAPI(retrofit: Retrofit): QuotesAPI {
    return retrofit.create(QuotesAPI::class.java)
}

/**
 * Provides the Retrofit object.
 * @return the Retrofit object
 */
@Provides
@Singleton
fun provideRetrofitInterface(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(MoshiConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .client(okHttpClient)
            .build()
}

}

ViewModelModule

@Module
abstract class ViewModelModule {

@Binds
abstract fun bindViewModelFactory(factory: MyViewModelFactory): ViewModelProvider.Factory

@Binds
@IntoMap
@ViewModelKey(QuotesListViewModel::class)
internal abstract fun bindsQuotesListViewModel(quotesListViewModel: QuotesListViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(QuoteListItemViewModel::class)
internal abstract fun bindsQuoteListItemViewModel(quoteListItemViewModel: QuoteListItemViewModel): ViewModel

}

MyApplication

class MyApplication : Application(), HasActivityInjector {

@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

override fun activityInjector(): AndroidInjector<Activity> = dispatchingAndroidInjector

override fun onCreate() {
    super.onCreate()
    // StateSaver.setEnabledForAllActivitiesAndSupportFragments(this, true)

    // initialise dependency injection
    DaggerAppComponent
        .builder()
        .application(this)
        .build()
        .inject(this)

}

override fun attachBaseContext(base: Context) {
    super.attachBaseContext(base)
    MultiDex.install(this)
}

}

MainActivity

class MainActivity : DaggerAppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var binding: ActivityMainBinding
private lateinit var menu: Menu

override fun onCreate(savedInstanceState: Bundle?) {
    AndroidInjection.inject(this)
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
}
}

1 个答案:

答案 0 :(得分:1)

最后,经过几次尝试,我在 AppComponent 中发现了我的错误。 我应该呼叫 MyApplication 类,而不是 Application

@Singleton
@Component(
modules = [AndroidSupportInjectionModule::class,
AppModule::class, NetworkModule::class,
ActivityBuilder::class]
)
interface AppComponent : AndroidInjector<MyApplication> {

@Component.Builder
interface Builder {
@BindsInstance
fun application(application: MyApplication): Builder

fun build(): AppComponent
}