Failed to run AsyncTask on DatePickerDialog


This is my code which I originally used, in this code I try to execute an asynchronous method in a DatePickerDialog

public static class DatePickerFragment extends DialogFragment //Se modifica a metodo estatico
        implements DatePickerDialog.OnDateSetListener {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT , this, year, month, day);

    public void onDateSet(DatePicker view, int y, int m, int d) {
        year = y;
        month = m;
        day = d;

        globalVariable.calendar.set(year, m, day);
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        fechaSelected = sdf.format(globalVariable.calendar.getTime());

        new getEventsAyncTask().execute(); //método que no es estático 

The problem is that you need the method to be static to run it inside the pickerdialog some idea of how to do it without having to change the asynchronous method to static?

asked by Alexis Caballero 19.07.2017 в 17:04

2 answers


The error is caused by the fact that the getActivity variable does not exist, probably what you are looking for, is to execute the function getActivity ().

answered by 19.07.2017 / 18:50

The error message:


Error: (373, 27) error: can not find symbol variable getActivity

indicates that the variable getActivity does not exist. Actually, what is needed is the method getActivity ()

If you are within Fragment , use as context the Activity containing the Fragment , using getActivity () :

Remember that getSupportFragmentManager() is a method of the Activity:


This would be your code:

 for (Fragment f : getActivity().getSupportFragmentManager().getFragments()) {

If you want to use the variable getActivity (which is not common) you can do this, getting the context and storing it in the variable getActivity :

 Context getActivity = getActivity(); 

     for (Fragment f : getActivity.getSupportFragmentManager().getFragments()) {
answered by 19.07.2017 в 19:48