Java “没有为类注册的注入”,即使我声明了它

Java “没有为类注册的注入”,即使我声明了它,java,android,dependency-injection,mvp,dagger,Java,Android,Dependency Injection,Mvp,Dagger,我是Dagger的新手,正在试图弄清楚为什么没有注入特定的依赖项。尝试在片段中插入演示者时出现以下错误: 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 m

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

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

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

}
对象图是在使用Reviews模块和injected module创建BaseApplication对象的过程中创建的。list返回模块列表,当前为空TestReviewModule和ReviewModule。。。见上图

private void initializeObjectGraph() {
    Timber.d("Initializing application object graph!");
    og = ObjectGraph.create(Modules.list(this));
    og.inject(this);
}
在OverviewFragment中,我检索创建的对象图aonViewCreated,并执行以下操作将我的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问题后发现了这个问题,例如和一些文档

我的基本问题是对.plus方法缺乏理解。我没有意识到它会返回一个新的ObjectGraph,而不是修改调用它的图形。通过在我称为.plus的同一个ObjectGraph上执行.plus注入,我实际上是在尝试从我的ReviewModule中进行注入。。。它不知道OverviewFragment,因此也不知道错误

在概览片段中,我需要对以下内容进行更改:

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


这将返回一个包含OverviewModule和ReviewModule的新图形

匕首/DI故障排除有点困难,所以我会尽可能彻底。。。也许这会帮助其他人

我在阅读了其他一些SO问题后发现了这个问题,例如和一些文档

我的基本问题是对.plus方法缺乏理解。我没有意识到它会返回一个新的ObjectGraph,而不是修改调用它的图形。通过在我称为.plus的同一个ObjectGraph上执行.plus注入,我实际上是在尝试从我的ReviewModule中进行注入。。。它不知道OverviewFragment,因此也不知道错误

在概览片段中,我需要对以下内容进行更改:

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


这将返回一个包含OverviewModule和ReviewModule的新图形

嗨,我只是好奇删除线的文字。为什么?嗨,我只是对删除线的文字很好奇。为什么呢?