android-@Inject设置为不注入属性
作者:互联网
我正在尝试使用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,指出“ lateinit属性尚未初始化”:
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)
这是用于创建AppComponent和注入MyApp依赖项的AppInjector类:
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)
}
我在属性访问器上使用@Inject注释,如the documentation中所述,但似乎无法正常工作.我究竟做错了什么?
解决方法:
我目前无法对其进行测试,但是我认为这是因为您的财产是私有的. Dagger不使用反射,因此必须在类外部从其可见的位置才能使其运行.
(显式写出set对此无济于事,因为在这种情况下,它仅表示一个私有的setter.请注意,字段对于所有属性都是私有的,而非private属性则具有适当的可见性的getter / setter.)
您还应该能够仅注释属性而不是其设置方法,因此这将是最终结果:
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
如果您希望属性为私有,请考虑使用构造函数注入.
标签:dagger-2,kotlin,dependency-injection,android 来源: https://codeday.me/bug/20191111/2018557.html