我有这样的申请
class App : Application() , HasAndroidInjector {
lateinit var application: Application
@Inject
lateinit var androidInjector : DispatchingAndroidInjector<Any>
override fun androidInjector(): AndroidInjector<Any> = androidInjector
override fun onCreate() {
super.onCreate()
this.application = this
}
}那我就有了AppComponent
@Singleton
@Component(
modules = [
AndroidSupportInjectionModule::class,
LoginModule::class
]
)
interface AppComponent : AndroidInjector<App> {
@Component.Factory
interface Factory {
fun create(@BindsInstance application: App): AppComponent
}
}LoginModule是这样的
@Module
abstract class LoginModule {
@ContributesAndroidInjector(modules = [LoginDependencies::class])
abstract fun bindLoginFragment(): LoginFragment
}在我使用的onViewCreated of Fragment中
AndroidSupportInjection.inject(this)我也尝试过将它添加到onAttach()上,但是它不起作用。一旦我添加了
@Inject
lateinit var loginPresenter: LoginContract.Presenter如果我删除它,它和问题一样,如果我离开演示者,它说没有初始化预置器。
是不是我错过了什么?
编辑
我在哪里得到了这个错误
引发的
:kotlin.UninitializedPropertyAccessException: lateinit属性androidInjector尚未初始化
override fun androidInjector(): AndroidInjector<Any> = androidInjector
那我也会得到这个
com.testing.login.login.presentation.LoginFragment.onRequestInjection(LoginFragment.kt:31)的
AndroidSupportInjection.inject(this)
这个onRequestInjection位于onViewCreated中的一个AbstractFragment中,我也尝试过将它放在onAttach()上,但是没有什么改变。
我的build.gradle包含:
这个在:app中
api(LibrariesDependencies.DAGGER)
api(LibrariesDependencies.DAGGER_ANDROID)
api(LibrariesDependencies.DAGGER_ANDROID_SUPPORT)
kapt LibrariesDependencies.DAGGER_ANDROID_KAPT
kapt LibrariesDependencies.DAGGER_KAPT
kapt LibrariesDependencies.DAGGER_ANNOTATION_PROCESSOR登录:登录
api(LibrariesDependencies.DAGGER)
api(LibrariesDependencies.DAGGER_ANDROID)
api(LibrariesDependencies.DAGGER_ANDROID_SUPPORT)
kapt LibrariesDependencies.DAGGER_ANDROID_KAPT
kapt LibrariesDependencies.DAGGER_KAPT
kapt LibrariesDependencies.DAGGER_ANNOTATION_PROCESSOR发布于 2020-03-11 09:06:23
未初始化的属性是否发生在App类中?如果是这样的话,这可能意味着在onCreate类的App方法中缺少以下内容。
DaggerAppComponent
.factory()
.create(this)
.inject(this)发布于 2020-03-11 09:09:06
在您的应用程序类中,您定义了一个@Inject属性,但没有在任何地方初始化它。您需要做的是:首先将App声明为Dagger目的的入口点:
interface AppComponent : AndroidInjector<App> {
@Component.Factory
interface Factory {
fun create(@BindsInstance application: App): AppComponent
}
fun inject(app: App) // Let Dagger know your Application class with root dispatching injector
}然后实际创建AppComponent实例并使用它引导Dagger并注入DispatchingAndroidInjector实例:
override fun onCreate() {
super.onCreate()
this.application = this
DaggerAppComponent.factory().create(this).inject(this)
}https://stackoverflow.com/questions/60627262
复制相似问题