我有两种片段。其中一个是ListFragment
,另一个是ListWithFilterFragment
那个ListWithFilterFragment extend ListFragment
。对于这些ListWithFilterPresenter extend ListPresenter
片段,我也有两个主持人。因此,我为演示者定义了单独的模块,并在Dagger中定义了单独的组件。我想为这些片段注入正确的演示者,但出现错误
这是ListFragment
:
public class ListFragment<V extends ListView> extends BaseFragment<V, ListPresenter<V>> implements ListView {
@Inject
ListPresenter presenter;
@NonNull
@Override
public ListPresenter createPresenter() {
DaggerListComponent.builder().build().inject((ListFragment<ListView>) this);
return presenter;
}
这是
ListWithFilterFragment
:public class ListWithFilterFragment extends ListFragment<ListWithFilterView> implements ListWithFilterView {
@Inject
ListWithFilterPresenter presenter;
@NonNull
@Override
public ListWithFilterPresenter createPresenter() {
DaggerListWithFilterComponent.builder().build().inject(this);
return presenter;
}
}
这是
ListComponent
:@Singleton
@Component(modules = {ListPresenterModule.class,
ListInteractorModule.class})
public interface ListComponent {
void inject(ListFragment<ListView> fragment);
}
这是
ListWithFilterComponent
:@Singleton
@Component(modules = {ListWithFilterPresenterModule.class,
ListInteractorModule.class})
public interface ListWithFilterComponent {
void inject(ListWithFilterFragment fragment);
}
和我的主持人模块
@Module
public class ListWithFilterPresenterModule {
@Provides @NonNull @Singleton
public ListWithFilterPresenter providePresenter(ListInteractor interactor) {
return new ListWithFilterPresenter(interactor);
}
}
@Module
public class ListPresenterModule {
@Provides @NonNull @Singleton
public ListPresenter providePresenter(ListInteractor interactor) {
return new ListPresenter(interactor);
}
}
错误:(16,10)错误:如果没有在以下位置插入@ Provides-或@ Produces-annotated.method.com.sober.appModules.List.presenter.ListPresenter,则无法提供com.sober.appModules.List.presenter.ListPresenter。
com.sober.appModules.List.ui.ListFragment.presenter
com.sober.appModules.List.ui.ListWithFilterFragment插入com.sober.appModules.List.injection.ListWithFilterComponent.inject(片段)
最佳答案
匕首不进行部分注入。它可以提供所有对象,否则将无法提供而失败。
在您的情况下,ListWithFilterComponent
无法为ListPresenter
或ListWithFilterFragment
父项提供ListFragment
。
在行末查看注释
error: List.presenter.ListPresenter cannot be provided without an @Provides- or @Produces-annotated.method.
List.presenter.ListPresenter is injected at <- missing object
List.ui.ListFragment.presenter <- field in parent that is getting injected
List.ui.ListWithFilterFragment is injected at <- object it tries to inject
List.injection.ListWithFilterComponent.inject(fragment) <- component that's missing something
如果要保留当前设置,则还需要从
ListPresenter
提供ListWithFilterComponent
,否则,您将需要重构片段,以使它们不会彼此延伸。