我有一个应该处理所有通知并因此扩展 FirebaseMessagingService 的类:
class DefaultFCMHandler(): FirebaseMessagingService(), FCMHandler, KodeinAware {
private val _kodein: Kodein by closestKodein()
override val kodein = Kodein.lazy {
extend(_kodein)
}
private val authenticator: Authenticator by instance()
override fun onNewToken(token: String) {
// ...
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// ...
}
}
在应用程序类中,我将所有内容绑定如下:
class MyApplication : Application(), KodeinAware {
override val kodein = Kodein.lazy {
// ...
bind<Authenticator>() with singleton {
DefaultAuthenticator(
instance(),
instance(),
instance()
)
}
// ...
bind<FCMHandler>() with singleton {
DefaultFCMHandler()
}
}
}
问题是,无论我尝试什么,我总是会遇到以下异常:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.content.Context android.content.Context.getApplicationContext()'
我不明白,因为基于此它应该可以访问上下文(我将应用程序文件放入清单中)。
我在 github 上找到了这个,但推广的解决方案并没有解决问题。
我可能对 DI 的工作原理有一个普遍的误解。
完整的堆栈跟踪如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.gitlab.user, PID: 14300
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:118)
at org.kodein.di.android.ClosestKt.kodein(closest.kt:25)
at org.kodein.di.android.ClosestKt.access$kodein(closest.kt:1)
at org.kodein.di.android.ContextKodeinPropertyDelegateProvider$provideDelegate$1.invoke(closest.kt:39)
at org.kodein.di.android.ContextKodeinPropertyDelegateProvider$provideDelegate$1.invoke(closest.kt:38)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at de.gitlab.user.data.fcm.DefaultFCMHandler.get_kodein(Unknown Source:7)
at de.gitlab.user.data.fcm.DefaultFCMHandler.access$get_kodein$p(DefaultFCMHandler.kt:16)
at de.gitlab.user.data.fcm.DefaultFCMHandler$kodein$1.invoke(DefaultFCMHandler.kt:21)
at de.gitlab.user.data.fcm.DefaultFCMHandler$kodein$1.invoke(DefaultFCMHandler.kt:16)
at org.kodein.di.internal.KodeinImpl$Companion.newBuilder(KodeinImpl.kt:22)
at org.kodein.di.internal.KodeinImpl$Companion.access$newBuilder(KodeinImpl.kt:21)
at org.kodein.di.internal.KodeinImpl.<init>(KodeinImpl.kt:19)
at org.kodein.di.Kodein$Companion$lazy$1.invoke(Kodein.kt:447)
at org.kodein.di.Kodein$Companion$lazy$1.invoke(Kodein.kt:429)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at org.kodein.di.LazyKodein.getBaseKodein(Unknown Source:7)
at org.kodein.di.LazyKodein.getContainer(lateinit.kt:31)
at org.kodein.di.KodeinAwareKt$Instance$1.invoke(KodeinAware.kt:176)
at org.kodein.di.KodeinAwareKt$Instance$1.invoke(Unknown Source:4)
at org.kodein.di.KodeinProperty$provideDelegate$1.invoke(properties.kt:42)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at de.gitlab.user.data.fcm.DefaultFCMHandler.getAuthenticator(Unknown Source:7)
at de.gitlab.user.data.fcm.DefaultFCMHandler.sendRegistrationToServer(DefaultFCMHandler.kt:74)
at de.gitlab.user.data.fcm.DefaultFCMHandler.access$sendRegistrationToServer(DefaultFCMHandler.kt:16)
at de.gitlab.user.data.fcm.DefaultFCMHandler$submitToken$1.onComplete(DefaultFCMHandler.kt:50)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)