简体   繁体   中英

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

@Module

public class 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.

@Singleton
@Module
public abstract class ContextModule {

    @Binds
    abstract Context provideContext(Application application);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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