I have been using using dagger 2 in my project lately,
the problem is when I try to build my project, the presenter in my login activity which injected like below is null ,
and when I try to build the project
presenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method...
I don't understand what have I done wrong??, please someone help me with this,
thanks in advance.
Here is my Login Activity, the presenter here is null, which shows that, I've not injected it properly
@Inject
LoginPresenter presenter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
InjectHelper.getRootComponent().injectPresenter(this);
presenter.setProgressBarVisiblity(View.INVISIBLE);
}
this is my presenter module
@Module
public class PresenterModule {
private final LoginActivity activity;
public PresenterModule(LoginActivity activity) {
this.activity = activity;
}
@Provides
@Singleton
public LoginActivity providesView() {
return activity;
}
}
@Provides
@Singleton
public LoginPresenter providesPresenter()
{
return new LoginPresenter();
}
}
this inject helper class
public class InjectHelper {
private static RootComponent sRootComponent;
static {
initModules();
}
private static void initModules() {
sRootComponent = getRootComponentBuilder().build();
}
public static DaggerRootComponent.Builder getRootComponentBuilder() {
return DaggerRootComponent.builder();
}
public static RootComponent getRootComponent() {
if (sRootComponent == null) {
initModules();
}
return sRootComponent;
}
}
this is root component class
@Singleton
@Component(modules = {
PresenterModule.class
})
public interface RootComponent {
void injectLoginView(LoginPresenter loginPresenter);
}
you need to inform the dagger which views want to use injection, in your component. you must change the inject function code in your component to below:
void inject(LoginActivity activity);
for showing dagger what you want, you need to use @dagger annotation NOT by sending it as inject function in component file. as you did properly:
@Inject
LoginPresenter presenter;
Dagger will search for a variable of type LoginPresenter in your module and finds the proper provider method using the type.
what you put in your component as a argument for "inject" function tells the Dagger what view you are going to do injection in (NOT what you want to inject)
@Singleton
@Component(modules = {PresenterModule.class})
public interface RootComponent {
void inject(LoginActivity activity);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.