Inject Presenter Into Activity Via Dagger
Solution 1:
I assume current error will be easy to fix, because it very much looks like this issue.
You've instructed Dagger how to provide @AScope CategoryContract.CategoryPresenter
but in activity you request Dagger to inject CategoryContract.CategoryPresenter
. Dagger is confused at this point, because those two things do not match.
What you have to do, is simply add @AScope
to categoryPresenter
:
@Inject@AScope
CategoryPresenter categoryPresenter;
I've checked out your project. The problem was in NetComponent.java
:
@Singleton@Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
voidinject(MainActivity activity);
}
The issue is in line void inject(MainActivity activity)
. Why is this an issue? Because by the time you construct NetComponent
in App
, dagger analyzes MainActivity
for @Inject
annotated field and sees CategoryPresenter
. This means, that at this point Dagger should find appropriate provider/binder method in order to be able to inject MainActivity
as declared inside this interface.
But it cannot find such a provider/binder method, because it is declared in an another component - CategoryPresenterComponent
and this component (NetComponent
) is not connected with CategoryPresenterComponent
anyhow (subcomponents or component dependencies), thus bindings are not exposed.
Simply removing that line will make your build successful:
@Singleton@Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
}
For resolving "Error:cannot access Nullable" refer to this thread, which suggest applying compile 'com.google.code.findbugs:jsr305:3.0.2'
to your gradle file.
After doing this you'll overcome that error message and will stumble on retrofit2.Retrofit cannot be provided without an @Inject constructor or an @Provides-annotated method
, which has the same symptoms as CategoryPresenter
had.
To overcome this issue you have to add a Retrofit
provider method inside NetComponent
(or to remove @Inject Retrofit retrofit
from activity):
@Singleton@Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
RetrofitprovidesRetrofit();
}
After doing this you'll be able to run the app, but you'll end up in a NPE in MainActivity
, because of this line:
.categoryContractModule(newCategoryContractModule(retrofit.create(HeadyAPI.class)))
You are referring to retrofit
object, which is not yet initialized.
Long story short, your initial question has mutated a couple of times and in fact you had a few problems in your code. Still you have a problem there, because you are trying to use retrofit
in order to construct a component, that was supposed to inject the retrofit
for you.
Post a Comment for "Inject Presenter Into Activity Via Dagger"