我在没有上下文的情况下使用 Dagger 2 注入适配器并且它正在工作,但是当我传递上下文参数时我无法执行此操作。错误是这样来的
error: android.content.Context cannot be provided without an @Provides-annotated method.
匕首组件
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = MainFragmentModule.class)
public interface MainFragmentComponent {
void inject(MainFragment mainFragment);
@ActivityContext
Context provideContext();
}
片段模块
@Module
public class MainFragmentModule {
private MainFragmentContract.View mView;
private Activity mActivity;
Context mContext;
MainFragmentModule(MainFragmentContract.View view, Context context) {
mView = view;
mContext = context;
}
@Provides
MainFragmentContract.View providesView() {
return mView;
}
@Provides
@ActivityContext
Context provideContext() {
return mContext;
}
}
Adapter
@Inject
public ConversationAdapter(MainFragmentPresenter mainPresenter, Context context) {
mMainFragmentPresenter = mainPresenter;
mContext =context;
}
您已经告诉 dagger,您正在提供特定的上下文:
@ActivityContext
Context provideContext();
然后你要求 dagger 向你的适配器注入另一种类型的上下文 - 一个,它没有用注释@ActivityContext
.
相反,您应该明确定义您愿意提供该类型的上下文:
@Inject
public ConversationAdapter(..., @ActivityContext Context context) {
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)