2015-05-30 4 views
11

Ich habe folgendes einfaches Modul:Der Kontext kann nicht ohne eine Methode mit @Announcated bereitgestellt werden, aber es ist?

@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; 
    } 

} 

und die jeweiligen einfache Komponente:

@Singleton 
@Component(
     modules = ApplicationModule.class 
) 
public interface ApplicationComponent { 

    CustomApplication getCustomApplication(); 

    Context getApplicationContext(); 

} 

Und Ich schaffe die Komponente hier:

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(); 

Es wirft diesen Fehler an Kompilierzeit: Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method, aber wie Sie sehen können, ist es mit @Provides annotiert.

Es ist wirklich seltsam, weil das Problem verschwindet, wenn ich die Qualifizierungsnotiz abnehme.

Nur für den Fall, das ist mein @ForApplication Qualifier:

@Qualifier @Retention(RUNTIME) 
public @interface ForApplication { 
} 

Das ist praktisch ein Lehrbuch Dagger2 Beispiel. Was mache ich falsch?

Antwort

21

Nach einer ganzen Weile von Versuch und Irrtum habe ich die Ursache gefunden, es ist die Mehrdeutigkeit von Context, weil @ForApplication in einigen Orten fehlt, wo Context benötigt wird.

Auch mag es mein schwaches Verständnis von Dagger2 im Moment sein, aber dieses Boilerplate ist ziemlich anfällig für Entwicklerfehler.

Wie auch immer ... für alle, die das Problem findet man muss nur die Qualifier Anmerkungen an jedem Ort hinzufügen, dass die Abhängigkeit verwendet wird:

@Singleton 
@Component(
     modules = ApplicationModule.class 
) 
public interface ApplicationComponent { 

    CustomApplication getCustomApplication(); 

    @ForApplication Context getApplicationContext(); 

} 
+0

Hier gilt das gleiche Problem. Ich musste meinem Konstruktor '@ ForApplication' hinzufügen:' @Inject MyThing (@ForApplication Context context) {this.context = context; } ' – anon

+0

Das macht keinen Sinn. Wo rufst du eigentlich 'getApplicationContext()' aus ?? –

+0

Könnten Sie näher erläutern, was genau keinen Sinn ergibt? Ich rufe diese Methode nicht an, das ist nur eine Deklaration für die Dagger2-Komponente. Ich sage nur, dass die Anmerkung in der Deklaration von "Context" in diesem Bereich fehlte. Deshalb habe ich den Fehler erhalten, weil mein Modul angegeben hat, dass der angegebene Kontext '@ForApplication' ist und meine Komponente nicht. – Eddnav

Verwandte Themen