简体   繁体   中英

inheritance and generic and Dagger2

I have two kind of fragment. One of them is ListFragment and the other one is ListWithFilterFragment that ListWithFilterFragment extend ListFragment . Also I have two presenter for these fragments that ListWithFilterPresenter extend ListPresenter . So I define separate module for presenter and separate component in Dagger. I want to inject correct presenter to these fragment but I got error

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

this is ListWithFilterFragment :

public class ListWithFilterFragment extends ListFragment<ListWithFilterView> implements ListWithFilterView {
    @Inject
    ListWithFilterPresenter presenter;

    @NonNull
    @Override
    public ListWithFilterPresenter createPresenter() {
        DaggerListWithFilterComponent.builder().build().inject(this);
        return presenter;
    }
}

this is ListComponent :

@Singleton
@Component(modules = {ListPresenterModule.class, 
ListInteractorModule.class})
public interface ListComponent {
    void inject(ListFragment<ListView> fragment);
}

this is ListWithFilterComponent :

@Singleton
@Component(modules = {ListWithFilterPresenterModule.class, 
ListInteractorModule.class})
public interface ListWithFilterComponent {
    void inject(ListWithFilterFragment fragment);
}

and my presenters modules

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

Error:(16, 10) error: com.sober.appModules.List.presenter.ListPresenter cannot be provided without an @Provides- or @Produces-annotated.method.com.sober.appModules.List.presenter.ListPresenter is injected at com.sober.appModules.List.ui.ListFragment.presenter com.sober.appModules.List.ui.ListWithFilterFragment is injected at com.sober.appModules.List.injection.ListWithFilterComponent.inject(fragment)

Dagger does not do partial injections. Either it can provide all the objects, or it will fail with cannot be provided .

In your case ListWithFilterComponent cannot provide ListPresenter to ListWithFilterFragment —or rather its ListFragment parent.

see comments at the end of the lines

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

If you want to keep your current setup you will need to provide ListPresenter from your ListWithFilterComponent as well, or otherwise you will need to refactor your fragments so that they don't extend one another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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