我正在尝试使用Android Dagger ‡ implementation并在我的DispatchingAndroidInjector
课程中注入Application
:
class MyApp : Application(), HasActivityInjector {
private lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject set
override fun onCreate() {
super.onCreate()
AppInjector.init(this)
}
override fun activityInjector(): ActivityInjector<Activity> {
return dispatchingAndroidInjector
}
}
但是我得到一个IllegalArgumentExecption
声明&#34; lateinit
属性尚未初始化&#34;:
06-19 10:57:30.773 10797-10797/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 10797
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ui.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property dispatchingAndroidInjector has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dispatchingAndroidInjector has not been initialized
at com.example.app.MyApp.activityInjector(MyApp.kt:28)
at dagger.android.AndroidInjection.inject(AndroidInjection.java:55)
at com.example.app.injection.AppInjector.handleActivity(AppInjector.kt:41)
at com.example.app.injection.AppInjector.access$handleActivity(AppInjector.kt:14)
at com.example.app.injection.AppInjector$init$1.onActivityCreated(AppInjector.kt:21)
at android.app.Application.dispatchActivityCreated(Application.java:197)
at android.app.Activity.onCreate(Activity.java:961)
at android.support.v4.app.BaseFragmentActivityGingerbread.onCreate(BaseFragmentActivityGingerbread.java:54)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
at com.example.app.ui.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:6682)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
这是用于创建AppInjector
并注入AppComponent
依赖项的MyApp
类:
object AppInjector {
fun init(app: MyApp) {
DaggerAppComponent.builder().application(app).build().inject(app)
// ...
}
}
AppComponent
界面:
@Singleton
@Component(modules = arrayOf(
AndroidInjectionModule::class,
AppModule::class,
MainActivityModule::class
)) interface AppComponent {
@Component.Builder interface Builder {
@BindsInstance fun application(app: Application): Builder
fun build(): AppComponent
}
fun inject(app: MyApp)
}
我在the documentation中概述了在属性访问器上使用@Inject
注释,但它似乎无法正常工作。我做错了什么?
答案 0 :(得分:3)
我现在无法对其进行测试,但我认为这是因为您的财产是私有的。 Dagger没有使用反射,所以它注入的字段必须从类外部可见才能运行。
(写出set
显然对此没有帮助,因为在这种情况下它只代表一个私有的setter。请注意,所有属性的字段都是私有的,非私有属性有getter / setters酌情提供更广泛的可见性。)
你也应该能够只注释属性而不是它的setter,所以这将是最终的结果:
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
如果您希望您的属性是私有的,请考虑使用构造函数注入。
答案 1 :(得分:0)
请勿使用 lateinit ,而应使用内部。
更改此
private lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject set
到
private internal var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject set