我有一个使用自定义应用程序类的Android应用程序,它看起来像这样:
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:name=".MyApplication"
tools:ignore="UnusedAttribute">
MyApplication.kt
class MyApplication : Application(), HasActivityInjector, HasSupportFragmentInjector, HasServiceInjector {
@Inject
lateinit var serviceInjector: DispatchingAndroidInjector<Service>
@Inject
lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>
@Inject
lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject
lateinit var remoteConfig: FirebaseRemoteConfig
@Inject
lateinit var workerFactory: GrowWorkerFactory
override fun activityInjector(): AndroidInjector<Activity> = activityDispatchingAndroidInjector
override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector
override fun serviceInjector(): AndroidInjector<Service> = serviceInjector
}
这是我的匕首组件类
interface MyApplicationComponent {
@Component.Builder
interface Builder {
fun build(): MyApplicationComponent
@BindsInstance
fun application(application: Application): Builder
fun dataSourceModule(module: DataSourceModule): Builder
fun serviceModule(module: ManagerModule): Builder
}
fun inject(app: MyApplication)
}
构建正常完成,应用程序启动,但是,当到达SplashActivity类时,应用程序崩溃并抛出以下错误:
2019-05-07 13:13:32.314 14002-14002/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app.debug, PID: 14002
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app.debug/com.my.app.debug.SplashActivity}: java.lang.RuntimeException: android.app.Application does not implement dagger.android.HasActivityInjector
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3484)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:7470)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.RuntimeException: android.app.Application does not implement dagger.android.HasActivityInjector
at dagger.android.AndroidInjection.inject(AndroidInjection.java:49)
at com.my.app.SplashActivity.onCreate(SplashActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:7436)
at android.app.Activity.performCreate(Activity.java:7426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3484)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:7470)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
该应用似乎忽略了MyApplication类