如果没有@Provides注释的方法,则无法提供Dagger 2 DashboardMenuContract.OnGetAllDashboardMenuListener

时间:2018-12-19 09:11:13

标签: android dagger-2 mvp

我发现了许多类似的问题,但是没有人能解决我的问题,有人可以指导我做错了什么。我正在努力消除过去两天的错误,但没有运气。

这是我的代码设置。

DashboardMenuContract.java

public interface DashboardMenuContract {

interface View{
void getAllDashboardMenuSucces(ArrayList<DashboardmenuModel> dashboardmenuModels);
void getAllDashboardMenuFailure(String message);
}
interface Presenter{
void getDashboardMenus();
}
interface Interactor{
void getAllDashboardMenusFromApi();
}

interface OnGetAllDashboardMenuListener{
void onGetAllDashboardMenuSucces(ArrayList<DashboardmenuModel> dashboardmenuModels);
void onGetAllDashboardMenuFailure(String message);
}
}

DashboardMenuPresenter.java

 public class DashboardMenuPresenter implements DashboardMenuContract.Presenter, DashboardMenuContract.OnGetAllDashboardMenuListener {

        private DashboardMenuContract.View view;
        private DashboardMenuInteractor dashboardMenuInteractor;

        @Inject
        public DashboardMenuPresenter(DashboardMenuContract.View view,DashboardMenuInteractor dashboardMenuInteractor) {
            this.view = view;
            this.dashboardMenuInteractor = dashboardMenuInteractor;
        }
}

DashboardMenuInteractor.java

public class DashboardMenuInteractor implements DashboardMenuContract.Interactor {
    private final ArrayList<DashboardmenuModel> dashboardmenuModelList = new ArrayList<>();
    DashboardMenuContract.OnGetAllDashboardMenuListener onGetAllDashboardMenuListener;
    @Inject
    public DashboardMenuInteractor(DashboardMenuContract.OnGetAllDashboardMenuListener onGetAllDashboardMenuListener) {
        this.onGetAllDashboardMenuListener = onGetAllDashboardMenuListener;
    }
}

MainActivityMvpModule.java

@Module
public class MainActivityMvpModule {
    private  DashboardMenuContract.View mView; 

    public MainActivityMvpModule(DashboardMenuContract.View mView) {
        this.mView = mView; 
    }

    @Provides
    @ActivityScope
    public DashboardMenuContract.View providesView(){
        return mView;
    } 



    @Provides
    @ActivityScope
    DashboardMenuContract.Interactor provideInteractor(DashboardMenuContract.OnGetAllDashboardMenuListener onGetAllDashboardMenuListener){
        return new DashboardMenuInteractor(onGetAllDashboardMenuListener);
    }


    @Provides
    @ActivityScope
    DashboardMenuPresenter providePresenter(DashboardMenuContract.View mView,DashboardMenuInteractor dashboardMenuInteractor){
        return new DashboardMenuPresenter(mView,dashboardMenuInteractor);
    } 
}

错误跟踪

E:\AndroidStudio\Motivation\app\src\main\java\motivation\com\motivation\component\DashboardMenuComponent.java:18: error: motivation.com.motivation.core.dashboardmanu.DashboardMenuContract.OnGetAllDashboardMenuListener cannot be provided without an @Provides-annotated method.
    void injectMainActivity(MainActivity mainActivity);
         ^
      motivation.com.motivation.core.dashboardmanu.DashboardMenuContract.OnGetAllDashboardMenuListener is injected at
          motivation.com.motivation.core.dashboardmanu.DashboardMenuInteractor.<init>(onGetAllDashboardMenuListener)
      motivation.com.motivation.core.dashboardmanu.DashboardMenuInteractor is injected at
          motivation.com.motivation.module.MainActivityMvpModule.providePresenter(…, dashboardMenuInteractor)
      motivation.com.motivation.core.dashboardmanu.DashboardMenuPresenter is injected at
          motivation.com.motivation.MainActivity.dashboardMenuPresenter
      motivation.com.motivation.MainActivity is injected at
          motivation.com.motivation.component.DashboardMenuComponent.injectMainActivity(mainActivity)

1 个答案:

答案 0 :(得分:0)

您应该在MainActivityMvpModule中提供侦听器。也许您应该投射dashboardMenuPresenter

@Provides
@ActivityScope
DashboardMenuContract.OnGetAllDashboardMenuListener provideListener(DashboardMenuPresenter dashboardMenuPresenter){
    return dashboardMenuPresenter;
}

编辑 因为使用我的答案,您会遇到Found a dependency cycle错误,这很有意义。我建议您在DashboardMenuContract.OnGetAllDashboardMenuListener onGetAllDashboardMenuListener构造函数中删除DashboardMenuInteractor,并为它提供setter方法。我认为这不是最好的解决方案,但是它将为您解决问题。缺点是您必须在Presenter中手动设置侦听器。