Dagger2 error: cannot be provided without @Inject constructor

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; } 
+5
source share
1 answer

First of all, as I said, your components do not have your provisioning methods. For instance,

  @Component(modules={AppModule.class}) @Singleton public interface AppComponent { Context context(); IntentStarter intentStarter(); } @Component(dependencies={AppComponent.class}), modules={MainActivityModule.class}) @ActivityScope public interface MainActivityComponent extends AppComponent { //other provision methods } 

And you make a mistake with the injection field, your IntentStarter should either call appComponent.inject(this) , or it should get your context in the constructor parameter.

In addition, I think that the annotated @Provides method needs a @Singleton to get a provider with scope, and labeling the module itself does virtually nothing.

So, to be specific,

 @Singleton @Component(modules = {AppModule.class}) public interface AppComponent { Application application(); Context provideContext(); EventBus provideEventBus(); IntentStarter provideIntentStarter(); } @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 @Singleton public EventBus provideEventBus() { return EventBus.getDefault(); } @Provides @Singleton public IntentStarter provideIntentStarter(Context context) { return new IntentStarter(context); } } @ActivityScope @Component(dependencies = {AppComponent.class}, modules={MainActivityModule.class}) public interface MainActivityComponent extends AppComponent { void inject(MainActivity mainActivity); } public class IntentStarter { private Context context; public IntentStarter(Context context) { this.context = context; } } 
+4
source

All Articles