[英]Method injection dagger2 null exception when asking for the dependency
嗨,我正在嘗試使用匕首2的方法注入,並在使用注入的依賴項時獲取空指針異常,我嘗試刪除@provide批注並在此處的單獨模塊中提取DialogManger
@Module
公共類DialogManagerModule {
Context context;
public DialogManagerModule(Context context){
this.context = context;
}
@Provides
DialogManager provideDialogManager(Context context){
return new DialogManager(context);
}
@Provides
ProgressDialogInteractor provideProgressDialogInteractor(){
return new ProgressDialog();
}
@Provides
Context provideContext(){
return context;
}
}
public class DialogManager {
Context context;
ProgressDialogInteractor progressDialog;
@Inject
public void setProgressDialog(ProgressDialogInteractor progressDialog) {
this.progressDialog = progressDialog;
}
public DialogManager(Context context) {
this.context = context;
}
public void showDatePickerDialog(DatePickerDialog.OnDateSetListener listener){
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(
context
, listener
, calendar.get(Calendar.YEAR)
, calendar.get(Calendar.MONTH)
, calendar.get(Calendar.DAY_OF_MONTH))
.show();
}
public void showProgressDialog(@Nullable String progressText){
progressDialog
/* .setText(progressText)
.showText()*/
.show();
}
}
這里是我如何稱呼組件
component =((App) getActivity().getApplication()).getComponent()
.newPersonalInfoFragmentComponent(new PersonalInfoFragmentModule(this), new DialogManagerModule(getContext()));
您應該像在ContextModule類中那樣提供上下文,並且必須提供在Provide方法中使用的所有對象。
@Singleton
@Module
public abstract class ContextModule {
@Binds
abstract Context provideContext(Application application);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.