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?
Hier gilt das gleiche Problem. Ich musste meinem Konstruktor '@ ForApplication' hinzufügen:' @Inject MyThing (@ForApplication Context context) {this.context = context; } ' – anon
Das macht keinen Sinn. Wo rufst du eigentlich 'getApplicationContext()' aus ?? –
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