Инъекция активности Androidx с помощью Dagger 2

1

Я новичок в инъекции зависимостей с Dagger в Android

У меня есть эта ошибка 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])

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

}

Мое выражение

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)
}

}

Основная деятельность

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)
}
}
Теги:
dependency-injection
dagger-2
dagger

1 ответ

0

Наконец, после нескольких попыток я нашел свою ошибку, в 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
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню