如果没有@Provides注释的方法,则无法提供Dagger AndroidInjector [英] Dagger AndroidInjector cannot be provided without an @Provides-annotated method
问题描述
我已经通过MVP模式完成了Android项目MainActivity.因此,在MainPresenter中,我想注入一个动态String,然后将其填充到TextView中,例如:
I've done my Android projects MainActivity by means of the MVP pattern. So in my MainPresenter I want to inject a dynamic String, which then shall populate a TextView for instance:
class MyMainPresenter @Inject constructor(@StringForTextView dynamicString : String )
而StringForTextView注释限定符定义为:
whereas the StringForTextView annotation qualifier is defined as:
import javax.inject.Qualifier
@Qualifier
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class StringForTextView
然后我也有我的界面:
interface DynamicString {
@NonNull fun getDynamicString() : String
}
实现为:
data class SimpleDynamicString(val dynString: String) : DynamicString {
override fun getDynamicString(): String { return dynString }
}
然后在我的模块中:
@Provides
@StringForTextView
static DynamicString provideDynamicString(Application application)
{
return new SimpleDynamicString(application.getString(R.string.example_string));
}
问题是,我得到一个错误
The problem is, that I get an error
错误:(51,2)错误:[dagger.android.AndroidInjector.inject(T)] @ com.ralf.example.injection.qualifiers.StringForTextView 如果没有@Provides注释,则无法提供java.lang.String 方法.
Error:(51, 2) error: [dagger.android.AndroidInjector.inject(T)] @com.ralf.example.injection.qualifiers.StringForTextView java.lang.String cannot be provided without an @Provides-annotated method.
我想念什么?
推荐答案
您正在提供DynamicString
,同时期望构造函数中有常规的String
.您也应该在其中使用DynamicString
:
You're providing a DynamicString
while expecting a regular String
in the constructor. You should use DynamicString
there as well:
class MyMainPresenter @Inject constructor(
@StringForTextView dynamicString: DynamicString
)
此外,您可能希望通过将其设为val
的属性来保留该注入的值.
Also, you might want to hols on to that injected value by making it a property with val
.
这篇关于如果没有@Provides注释的方法,则无法提供Dagger AndroidInjector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!