android - Dagger 2.26 字段注入应用程序失败
问题描述
当我尝试使用lateinit 属性注入应用程序崩溃时...尚未初始化异常。我知道我必须将我的应用程序注入图表,但我没有成功。做这个的最好方式是什么?
class App : Application() {
@Inject
lateinit var localeManager: Lazy<LocaleManager>
val appComponent: AppComponent by lazy {
DaggerAppComponent.factory().create(applicationContext)
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(localeManager.get().setLocale(base))
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
localeManager.get().setLocale(this)
}
}
@Singleton
@Component(modules = [NetworkModule::class, AppSubcomponents::class])
interface AppComponent {
@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context): AppComponent
}
fun registrationComponent(): RegistrationComponent.Factory
fun lobbyComponent(): PropertyComponent.Factory
fun supplierComponent(): SupplierComponent.Factory
fun notificationComponent(): NotificationComponent.Factory
fun serviceComponent(): ServiceComponent.Factory
fun settingsComponent() : SettingsComponent.Factory
fun cardComponent() : CardComponent.Factory
fun inject(activity: MainActivity)
}
class LocaleManager @Inject constructor(val storage: UserStorage) {
//some code
}
解决方案
推荐阅读
- regex - 如何在没有环视的情况下在 RegEx 中捕获“搜索”而不是“/搜索”?
- r - 解释 R 中 ETS() 和 AUTO.ARIMA() 模型的拟合值的问题
- junit - 如何为单元测试制作 JMS ObjectMessage?
- out-of-memory - 在看似无辜的 eks worker ami 升级后,Kong k8s 部署失败
- json - 在现有的 i 标签字段中调用 json 数据
- jenkins - Kubernetes - 如何运行 jenkins 的本地镜像
- time - (for (int i=0 ; i< pow(2,n) ; i++)
- python - 如何将数据框的列移动为多索引标题?
- laravel-7.x - 在 Laravel 7.x 中,Homestead 照片已正确上传到存储中,但在刀片文件中获取时未显示
- firemonkey - 更改 TIdTCPClient.Connect 中的错误消息