[英]Context cannot be provided without an @Provides-annotated method, but it is?
I have the following simple module: 我有以下简单的模块:
@Module
public class ApplicationModule {
private CustomApplication customApplication;
public ApplicationModule(CustomApplication customApplication) {
this.customApplication = customApplication;
}
@Provides @Singleton CustomApplication provideCustomApplication() {
return this.customApplication;
}
@Provides @Singleton @ForApplication Context provideApplicationContext() {
return this.customApplication;
}
}
And the respective simple component: 以及各自的简单组件:
@Singleton
@Component(
modules = ApplicationModule.class
)
public interface ApplicationComponent {
CustomApplication getCustomApplication();
Context getApplicationContext();
}
And I'm creating the component here: 我在这里创建组件:
public class CustomApplication extends Application {
...
private ApplicationComponent component;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
@Override
public void onCreate() {
super.onCreate();
component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
It throws this error at compile time: Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method
, but as you can see it is annotated with @Provides
. 它在编译时抛出此错误: Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method
@Provides
Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method
@Provides
Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method
, Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method
,但正如您所看到的,它是使用@Provides
注释的。
It's really strange because the problem goes away when I take the qualifier annotation off. 这真的很奇怪,因为当我取消限定符注释时问题就消失了。
Just in case, this is my @ForApplication
qualifier: 以防万一,这是我的@ForApplication
限定符:
@Qualifier @Retention(RUNTIME)
public @interface ForApplication {
}
This is practically a textbook Dagger2 example. 这实际上是教科书Dagger2的例子。 What am I doing wrong? 我究竟做错了什么?
After quite a while of trial and error I've seem to found the cause, it's the ambiguity of Context
because @ForApplication
is missing in some places where Context
is needed. 经过一段时间的反复试验,我似乎找到了原因,这是Context
的歧义,因为在需要Context
某些地方缺少@ForApplication
。
Also it may be my frail understanding of Dagger2 at the moment, but this boilerplate is quite prone to developer errors. 此外,它可能是我对Dagger2的脆弱理解,但这个样板很容易出现开发人员错误。
Anyhow... for anyone that finds the problem you just have to add the qualifier annotations in every place that dependency is used: 无论如何......对于发现问题的任何人,您只需在每个使用依赖项的地方添加限定符注释:
@Singleton
@Component(
modules = ApplicationModule.class
)
public interface ApplicationComponent {
CustomApplication getCustomApplication();
@ForApplication Context getApplicationContext();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.