1
我使用Dagger 2注入了一個無上下文的適配器,它正在工作,但是當我傳遞上下文參數時,我無法做到這一點。錯誤來了這樣使用匕首將上下文或活動傳遞給適配器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;
}
}
適配器
@Inject
public ConversationAdapter(MainFragmentPresenter mainPresenter, Context context) {
mMainFragmentPresenter = mainPresenter;
mContext =context;
}
您可以添加(?活動)的代碼,在使用的適配器?在這個類中包含Dagger初始化。 – Christopher
我無法添加片段代碼...當我點擊保存時發生錯誤。 – Jarvis
DaggerMainFragmentComponent.builder()。applicationComponent(((QTConnectApp)getActivity()。getApplication())。getComponent()) .mainFragmentModule(new MainFragmentModule(this,getContext())) .build().injection(this) ; – Jarvis