Search code examples
javaandroiddependency-injectionmvpdagger

"No inject registered for a class" even though I declare it


I'm new to Dagger and am trying to figure out why a particular dependency is not being injected. I'm getting the following error when attempting to inject a Presenter in a Fragment:

Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.company.myapp.view.fragment.OverviewFragment. You must explicitly add it to the 'injects' option in one of your modules.
            at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:302)
            at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279)
            at com.company.myapp.view.fragment.OverviewFragment.initializePresenter(OverviewFragment.java:50)
            at com.company.myapp.view.fragment.BaseFragment.onViewCreated(BaseFragment.java:28)
            at com.company.myapp.view.fragment.OverviewFragment.onViewCreated(OverviewFragment.java:84)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
            at android.app.Activity.onCreateView(Activity.java:4786)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.company.myapp.view.activity.HomeActivity.onCreate(HomeActivity.java:23)
            at android.app.Activity.performCreate(Activity.java:5231)

...

I have an ApplicationModule which pulls in the necessary app-wide modules (such as analytics).

@Module(
        injects = {
                BaseApplication.class,
                MyApplication.class,
                TestMyApplication.class
        },
        includes = { DomainModule.class }
)
public class MyModule {
    private final BaseApplication mApp;

    public MyModule(BaseApplication app) {
        mApp = app;
    }

    @Provides
    @Singleton
    public Context provideApplicationContext() {
        return mApp;
    }

}

I have an OverviewModel which directly correlates to an OverviewFragment. The whole purpose of this module is to inject the Presenter into the OverviewFragment.

@Module(
        injects = OverviewFragment.class,
        addsTo = ReviewsModule.class
)
public class OverviewModule {

    private OverviewView mView;

    public OverviewModule(OverviewView view) {
        mView = view;
    }

    @Provides
    @Singleton
    public OverviewView provideView() {
        return mView;
    }

    @Provides
    @Singleton
    public OverviewPresenter provideOverviewPresenter(OverviewView view) {
        return new OverviewPresenter(view);
    }

}

The object graph is created in the onCreate of a BaseApplication object (with the Reviews module) and injected (Modules.list() returns a list of modules, currently a blank TestReviewsModule and a ReviewsModule... seen above).

private void initializeObjectGraph() {
    Timber.d("Initializing application object graph!");
    og = ObjectGraph.create(Modules.list(this));
    og.inject(this);
}

In the OverviewFragment, I retrieve this created object graph a(onViewCreated) and do the following to add my OverviewModule to the application wide object graph:

public class OverviewFragment extends BaseFragment implements OverviewView {

    protected ObjectGraph og;
    @Inject OverviewPresenter mPresenter;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        og = BaseApplication.getInstance().getAppObjectGraph();
        initializePresenter();
    }

    @Override
    void initializePresenter() {
        og.plus(new OverviewModule(this));
        og.inject(this);
        mPresenter.initialize(mVendorId);
    }
}

Why is my application saying I haven't registered even though I explicitly state that I am injecting into OverviewFragment in my OverviewModule?


Solution

  • Dagger/DI troubleshooting is kind of difficult, so I'll be as thorough as possible... maybe this will help some others out.

    I found the problem after reading some other SO questions (such as Dagger can't find injectable members on a module) and some documentation.

    My foundational problem was a lack of understanding of the .plus() method. I didn't realize that it returns a NEW ObjectGraph instead of modifying the graph it was called on. By doing a .plus() and then injecting on the same ObjectGraph I called the .plus() on, I was essentially attempting to inject from my ReviewsModule... which has no knowledge of OverviewFragment and hence the error.

    In the OverviewFragment I needed to make the change from:

    // old way
    void initializePresenter() {
        og.plus(new OverviewModule(this)); // Note this line
        og.inject(this);
        mPresenter.initialize(mVendorId);
    }
    

    to

    // new way
    void initializePresenter() {
        og = og.plus(new OverviewModule(this)); // Note this line
        og.inject(this);
        mPresenter.initialize(mVendorId);
    }
    

    This returns a new graph with both OverviewModule and ReviewsModule.