抛出的错误或同时发生:
@Inject
@Named("DummyString")
lateinit var dummyString: String
@Inject
@Named("SomethingToRemember")
lateinit var sthToRemember: String
但以下方法可以:
@Inject
@Named("MySharedPrefs")
lateinit var mySharedPreferences: SharedPreferences
在Dagger中提供和使用String有什么特别的地方吗?
我的模块
@Provides
@JvmStatic
@Singleton
@Named("MySharedPrefs")
fun provideMySharedPrefs(context: Context) : SharedPreferences {
return context.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE)
}
@Provides
@JvmStatic
@Singleton
@Named("DummyString")
fun provideDummyString(): String {
return "dummy dummy"
}
@Provides
@JvmStatic
@Singleton
@Named("SomethingToRemember")
fun provideSthToRemember(@Named("MySharedPrefs") sharedPreferences: SharedPreferences) : String {
return sharedPreferences.getString("SomethingToRemember", "") ?: ""
}
错误详细信息:
error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface FormsPreviewAppComponent extends dagger.android.AndroidInjector<com.myapp.android.MyApplication> {
^
java.lang.String is injected at
com.myapp.android.MyLinkActivity.sthToRemember
com.myapp.android.MyActivity is injected at
dagger.android.AndroidInjector.inject(T) [com.myapp.android.MyAppComponent → com.myapp.android.MyAppModule_Bindable_ContributesMyActivity.MyActivitySubcomponent]
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':myapp:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
答案 0 :(得分:0)
我需要使用@field:Named("DummyString")
而不是@Named("DummyString")
https://github.com/google/dagger/issues/848