繁体   English   中英

即使我声明“没有为课程注册”

[英]“No inject registered for a class” even though I declare it

我是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在模块上找不到可注入的成员 )和一些文档之后,我发现了问题。

我的基本问题是对.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的新图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM