Reputation: 691
Sorry for my english, now i begin learn dagger2 and i cant understand why i have error:
Error:(9, 10) error: test.dagger.dagger.modules.MainActivityPresenterModule cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. test.dagger.dagger.modules.MainActivityPresenterModule is injected at test.dagger.view.activitys.MainActivity.mainActivityPresenterModule test.dagger.view.activitys.MainActivity is injected at test.dagger.dagger.components.AppComponent.injectMainActivity(mainActivity)
App
public class App extends Application {
private static AppComponent component;
@Override
public void onCreate() {
super.onCreate();
component = DaggerAppComponent.create();
}
public static AppComponent getComponent() {
return component;
}
}
AppComponent
@Component(modules = {MainActivityPresenterModule.class})
public interface AppComponent {
void injectMainActivity(MainActivity mainActivity);
}
MainActivityPresenterModule
@Module
public class MainActivityPresenterModule {
@Provides
MainActivityPresenter provideActivityPresenter(NetworkUtils networkUtils) {
return new MainActivityPresenter(networkUtils);
}
@Provides
NetworkUtils provideNetworkUtils(){
return new NetworkUtils();
}
}
NetworkUtils
public class NetworkUtils {
public boolean isConnection() {
return true;
}
}
MainActivityPresenter
public class MainActivityPresenter {
NetworkUtils networkUtils;
public MainActivityPresenter(NetworkUtils networkUtils) {
this.networkUtils = networkUtils;
}
public void getUser(){
if(networkUtils.isConnection()) {
Log.e("getUser", "getUser");
} else {
Log.e("no internet", "no internet connection");
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Inject
MainActivityPresenterModule mainActivityPresenterModule;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App.getComponent().injectMainActivity(MainActivity.this);
}
}
Upvotes: 0
Views: 271
Reputation: 2451
You can inject only the things that are provided in the classes annotated with @Module
(only methods inside that module which are annotated with @Provides
). So, you can do @Inject MainActivityPresenter presenter
, for instance, not try to inject the whole module, like you tried to do. Modules should be registered on Dagger initialisation, like this (in App#onCreate)
component = DaggerAppComponent.builder()
.mainActivityPresenterModule(MainActivityPresenterModule())
.build()
In MainActivity you only need to call inject to be able to inject your @Inject MainActivityPresenter presenter
or any other injects defined in the module, like so:
@Inject MainActivityPresenter presenter
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
(application as App).component.inject(this)
// after #inject(this) above you can start using your injections:
presenter.getUser()
}
Sorry, I wrote code snippets in Kotlin as it was much less to write that way, hopefully you get the idea how it looks in Java.
Upvotes: 2