我是Dagger的新手,正在尝试弄清楚为什么没有注入特定的依赖项。尝试将Presenter插入片段时出现以下错误:

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)


...

我有一个ApplicationModule,它引入了必要的应用程序级模块(例如分析)。

@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;
    }

}


我有一个OverviewModel直接与OverviewFragment相关。该模块的全部目的是将Presenter注入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);
    }

}


对象图是在BaseApplication对象的onCreate中创建的(带有Reviews模块)并被注入(Modules.list()返回模块列表,当前为空白的TestReviewsModule和ReviewsModule ...如上所示)。

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


在OverviewFragment中,我检索此创建的对象图a(onViewCreated),然后执行以下操作以将我的OverviewModule添加到应用程序范围的对象图中:

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);
    }
}


为什么我的应用程序说即使我明确声明要注入OverviewModule的OverviewFragment中,也没有注册?

最佳答案

Dagger / DI故障排除有点困难,所以我会尽可能的彻底...可能会帮助其他人。

阅读其他一些SO问题(例如Dagger can't find injectable members on a module)和一些文档后,我发现了问题。

我的基本问题是对.plus()方法缺乏了解。我没有意识到它返回一个新的ObjectGraph而不是修改它被调用的图。通过执行.plus(),然后在与我调用.plus()的对象相同的ObjectGraph上进行注入,我实质上是在尝试从我的ReviewsModule中注入...,它不了解OverviewFragment,因此不知道该错误。

在OverviewFragment中,我需要从以下位置进行更改:

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




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


这将返回带有OverviewModule和ReviewsModule的新图。

08-28 19:22