Ich habe eine Aktivität mit 2 Injektionen. Jede injizierte Komponente arbeitet allein, sondern die Injektion sowohl führt zu folgenden Fehler:Dolch 2 multiple Injektion auf Android
Error:(12, 10) error: android.app.Fragment cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
fr.daggertest.MainActivity.fragmentB
[injected field of type: android.app.Fragment fragmentB]
Error:(12, 10) error: android.app.Application cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
fr.daggertest.MainActivity.applicationA
[injected field of type: android.app.Application applicationA]
Aber sie sind beide schon @Provides kommentiert, so sehe ich nicht, was falsch ist?
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Inject
Fragment fragmentB;
@Inject
Application applicationA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create component & inject...
}
Module & Komponenten:
@Component(modules={ModuleA.class})
@Singleton
public interface ComponentA {
void inject(MainActivity activity);
}
@Component(modules={ModuleB.class})
@Singleton
public interface ComponentB {
void inject(MainActivity activity);
}
@Module
public class ModuleA {
Application mApplication;
public ModuleA(Application application) {
mApplication = application;
}
@Provides
public Application provideApplication() {
return mApplication;
}
}
@Module
public class ModuleB {
Fragment mFragment;
public ModuleB(Fragment fragment) {
mFragment = fragment;
}
@Provides
public Fragment provideFragment() {
return mFragment;
}
}
Sie haben keine Methode angegeben, die "Aktivität", nur "Fragment" und "Anwendung" bereitstellt. Könnten Sie auch Ihren Code "create component & inject" posten? – Pelocho
Sorry, es war ein Tippfehler, ich habe den Code bearbeitet. Der Code "create component & inject" ist auskommentiert, das ist nicht die Ursache des Kompilierungsfehlers. –
Eigentlich könnte es. Überprüfen Sie meine Antwort – Pelocho