如果没有 @Inject 构造函数或来自 @Provides- 或 @Produces- 注释的方法,则无法提供获取。在 kotlin dagger2 上
Getting cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. on kotlin dagger2
error: app.sareing.core.sharedPrefs.StringPreference cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
public abstract void inject(@org.jetbrains.annotations.NotNull()
^
app.sareing.core.sharedPrefs.StringPreference is injected at
app.sareing.activity.MainActivity.authToken
app.sareing.activity.MainActivity is injected at
app.sareing.injection.component.ActivityComponent.inject(activity)
已尝试命名字段,仍然无法解决此问题。
在 Dagger 中,您可以通过两种方式提供对象
- 在构造中使用@Inject
示例:
UserRepository @Inject constructor(private val mContext: Context)
- 但是我们如何使用第三方的一些class @Inject 构造函数,比如 Retrofit Service Interface?这就是 Dagger 有 @Provide
的原因
示例:
@Module class AppModule {
@Provides
fun provideContext(application: Application): Context = application.applicationContext }
Dagger 还有一些其他方式来提供对象
你可以阅读更多here
在您的情况下,您可以在您的应用程序模块中提供您的 StringPreference class,例如 AppModule
@Module
class AppModule {
@Provides
fun provideStringPreference(context: Context): StringPreference = StringPreference(context)
}
已通过此处的解决方案修复
我在注射时没有使用@fields:qualifier_name。
谢谢
error: app.sareing.core.sharedPrefs.StringPreference cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
public abstract void inject(@org.jetbrains.annotations.NotNull()
^
app.sareing.core.sharedPrefs.StringPreference is injected at
app.sareing.activity.MainActivity.authToken
app.sareing.activity.MainActivity is injected at
app.sareing.injection.component.ActivityComponent.inject(activity)
已尝试命名字段,仍然无法解决此问题。
在 Dagger 中,您可以通过两种方式提供对象
- 在构造中使用@Inject
示例:
UserRepository @Inject constructor(private val mContext: Context)
- 但是我们如何使用第三方的一些class @Inject 构造函数,比如 Retrofit Service Interface?这就是 Dagger 有 @Provide 的原因
示例:
@Module class AppModule {
@Provides
fun provideContext(application: Application): Context = application.applicationContext }
Dagger 还有一些其他方式来提供对象
你可以阅读更多here
在您的情况下,您可以在您的应用程序模块中提供您的 StringPreference class,例如 AppModule
@Module
class AppModule {
@Provides
fun provideStringPreference(context: Context): StringPreference = StringPreference(context)
}
已通过此处的解决方案修复
我在注射时没有使用@fields:qualifier_name。
谢谢