gpt4 book ai didi

java - 如何注入(inject)Presenter和View界面MVP dagger 2

转载 作者:行者123 更新时间:2023-12-02 11:07:34 29 4
gpt4 key购买 nike

您好,我正在使用 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)

以下是类别契约(Contract)

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

以下是主讲人

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

网络组件

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


}

类别Presenter组件

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

}

注入(inject)代码

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

我在模块中使用 Provides 提供 View ,但仍然收到错误消息 知道为什么吗?

最佳答案

NetworkComponent 中删除 voidject(MainActivity Activity) 将解决该问题。查看详细解答here .

关于java - 如何注入(inject)Presenter和View界面MVP dagger 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50851159/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com