简体   繁体   English

没有@Annex-annotated方法就无法提供上下文,但它是?

[英]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 methodError:(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.

相关问题 没有@Provides注释的方法无法提供 - Cannot be Provided Without an @Provides-annotated Method 如果没有@Provides 注释的方法 Dagger/MissingBinding,则无法提供 - cannot be provided without an @Provides-annotated method Dagger/MissingBinding Android匕首2:如果没有@Provides注释的方法,则无法提供依赖项 - Android dagger 2: dependency cannot be provided without an @Provides-annotated method 不能在没有 @Provides 注释的方法的情况下提供(Android Kotlin 中的 Dagger 2) - Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin) Dagger 2错误:依赖项“如果没有@Provides注释的方法就无法提供” - Dagger 2 error: dependency “cannot be provided without an @Provides-annotated method" 错误:如果没有 @Provides-annotated 方法就无法提供 - Error: cannot be provided without an @Provides-annotated method 匕首柄:如果没有@Provides-annotated 方法,则无法提供 - Dagger Hilt: cannot be provided without an @Provides-annotated method android.content.Context 不能在没有@Provides-annotated 方法的情况下提供 - android.content.Context cannot be provided without an @Provides-annotated method 没有@Provides注释的方法就无法提供对象 - Objects cannot be provided without an @Provides-annotated method 没有@Provides注释的方法,无法提供该类 - The class cannot be provided without an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM