gpt4 book ai didi

android - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供 java.lang.String

转载 作者:行者123 更新时间:2023-12-04 23:58:05 25 4
gpt4 key购买 nike

抛出的错误或两者:

    @Inject
@Named("DummyString")
lateinit var dummyString: String

@Inject
@Named("SomethingToRemember")
lateinit var sthToRemember: String

但以下是好的:

    @Inject
@Named("MySharedPrefs")
lateinit var mySharedPreferences: SharedPreferences

String with Dagger 的提供和使用有什么特别之处吗?

我的模块

    @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)

最佳答案

我需要使用 @field:Named("DummyString") 而不是 @Named("DummyString") https://github.com/google/dagger/issues/848

关于android - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58688521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com