
@Modulepublic 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; }} 和相应的简单组件:
@Singleton@Component( modules = ApplicationModule.class)public interface ApplicationComponent { CustomApplication getCustomApplication(); Context getApplicationContext();} 我在这里创建组件:
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(); 它会在编译时抛出此错误:错误:(22,13)错误:如果没有@ ProvIDe-annotated方法,则无法提供androID.content.Context,但是您可以看到它使用@ProvIDes进行注释.
这真的很奇怪,因为当我将限定符注释关闭时,问题消失了.
以防万一,这是我的@ForApplication限定词:
@QualifIEr @Retention(RUNTIME)public @interface ForApplication {} 这实际上是一本教材Dagger2的例子.我究竟做错了什么?
解决方法 经过一段时间的尝试和错误,我似乎发现了原因,这是Context的含糊之处,因为在需要Context的某些地方,缺少@ForApplication.此外,这可能是我对Dagger2的虚弱的理解,但是这个样板很容易出现开发者的错误.
无论如何…对于发现问题的任何人,您只需要在使用依赖关系的每个地方添加限定符注释:
@Singleton@Component( modules = ApplicationModule.class)public interface ApplicationComponent { CustomApplication getCustomApplication(); @ForApplication Context getApplicationContext();} 总结 以上是内存溢出为你收集整理的android – 如果没有@ Provide-annotated方法,则无法提供上下文,但它是?全部内容,希望文章能够帮你解决android – 如果没有@ Provide-annotated方法,则无法提供上下文,但它是?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)