I am completely new to Dagger 2 and have a little problem. Hope you can help me :) I have the following classes in my Android project.
- applications
- AppComponent
- Appmodule
- Mainactivity
- Maincompponent
- Mainmodule
- IntentStarter
When restoring / compiling, I get an error
Error:(15, 10) error: xyz.IntentStarter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. xyz..MainActivity.intentStarter [injected field of type: xyz..IntentStarter intentStarter]
I tried many options, but to no avail ... I tried it with a constructor in the IntentStarter class .. without a constructor ...: / Now some code ...
// AppComponent.class @Singleton @Component(modules = {AppModule.class}) public interface AppComponent { // Empty... }
...
// AppModule.class @Singleton @Module public class AppModule { Application application; Context context; public AppModule(Application app) { this.application = app; this.context = app.getApplicationContext(); } @Provides public Application provideApplication() { return application; } @Provides public Context provideContext() { return context; } @Provides public EventBus provideEventBus() { return EventBus.getDefault(); } @Provides public IntentStarter provideIntentStarter() { return new IntentStarter(); } }
...
// App.class public class App extends Application { public AppComponent appComponent; public AppComponent getAppComponent() { return appComponent; } @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent.builder() .appModule(new AppModule(this)) .build(); } }
...
//MainAcitivty.class public class MainActivity extends MosbyActivity { @Inject IntentStarter intentStarter; MainActivityComponent component; @Override protected void injectDependencies() { component = DaggerMainActivityComponent.builder() .appComponent(((App) getApplication()).getAppComponent()) .build(); component.inject(this); } }
...
//MainActivityComponent.class @ActivityScope @Component(dependencies = {AppComponent.class}) public interface MainActivityComponent { void inject(MainActivity mainActivity); }
...
// MainActivityModule @Module public class MainActivityModule { }
...
//IntentStarter public class IntentStarter { @Inject Context context; }
source share