如何注入Preseneter和View界面MVP dagger 2

时间:2018-06-14 06:44:26

标签: java android dependency-injection dagger-2 dagger

您好我正在使用匕首并且无法构建项目以下是我收到的错误消息

Error:(18, 10) error: [Dagger/MissingBinding] 
com.headytest.android.category_listing.CategoryContract.CategoryView cannot 
be provided without an @Provides-annotated method.
com.headytest.android.category_listing.CategoryContract.CategoryView is 
injected at
com.headytest.android.category_listing.CategoryPresenterImpl.<init> 
(categoryView)
com.headytest.android.category_listing.CategoryPresenterImpl is injected at
com.headytest.android.MainActivity.categoryPresenter
com.headytest.android.MainActivity is injected at
com.headytest.android.dagger_component.NetComponent.inject
(com.headytest.android. MainActivity)

以下是CategoryContract

public interface CategoryContract {

interface CategoryPresenter {

    public void onStart();

    public void onStop();

    public void getCategoryLiast();

}

interface CategoryView {

    public void onPreAPIRequest();

    public void onAPISuccess();

    public void onAPIError();

    public void setCategoryList(Result result);
}
}

以下是Presenter

public class CategoryPresenterImpl implements CategoryContract.CategoryPresenter {
CategoryContract.CategoryView categoryView;
Retrofit retrofit;

@Inject
public CategoryPresenterImpl(CategoryContract.CategoryView categoryView) {
    this.categoryView = categoryView;
}

@Override
public void onStart() {

}

@Override
public void onStop() {

}

@Override
public void getCategoryLiast() {

}
}

以下是模块

@Module
public class CategoryContractModule {
private CategoryContract.CategoryView categoryView;

public CategoryContractModule(CategoryContract.CategoryView categoryView) {
    this.categoryView = categoryView;
}

@Provides
@AScope
CategoryContract.CategoryView providesCategoryView() {
    return this.categoryView;

}


@Provides
@AScope
CategoryPresenterImpl providesCategoryPresenter() {
    return new CategoryPresenterImpl(categoryView);
}
}

NetComponent

@Singleton
@Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
    void inject(MainActivity activity);


}

CategoryPresenterComponent

 @AScope
 @Component(dependencies = NetComponent.class, modules = 
 {CategoryContractModule.class})
 public interface CategoryPresenterComponent {
    void inject(MainActivity activity);

 }

注入代码

 DaggerCategoryPresenterComponent.builder()
            .netComponent(((App) getApplicationContext()).getNetComponent())
            .categoryContractModule(new CategoryContractModule(this))
            .build()
            .inject(this);

我在提供模块中提供视图仍然是我收到错误消息任何想法为什么?

1 个答案:

答案 0 :(得分:1)

void inject(MainActivity activity)删除NetworkComponent将解决此问题。请参阅详细答案here