简体   繁体   English

请求依赖项时方法注入dagger2空异常

[英]Method injection dagger2 null exception when asking for the dependency

Hi i'm trying to use method injection with dagger 2 and im getting null pointer exception when using the injected dependency i tried removing the @provide annotation and extracted the DialogManger in separate module here 嗨,我正在尝试使用匕首2的方法注入,并在使用注入的依赖项时获取空指针异常,我尝试删除@provide批注并在此处的单独模块中提取DialogManger

@Module

public class DialogManagerModule { 公共类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();

}

} }

and here how i called the component 这里是我如何称呼组件

component =((App) getActivity().getApplication()).getComponent()
            .newPersonalInfoFragmentComponent(new PersonalInfoFragmentModule(this), new DialogManagerModule(getContext()));

You should provide your context like that in ContextModule class, and you have to provide all objects that you use in provide methods. 您应该像在ContextModule类中那样提供上下文,并且必须提供在Provide方法中使用的所有对象。

@Singleton
@Module
public abstract class ContextModule {

    @Binds
    abstract Context provideContext(Application application);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM