0

I'm trying to create an AsyncTask with Context as parameter onClick of a button of a Alert.

But I am getting the error:

No enclosing instance of type is accessible Alerts. Must Qualify the allocation With An enclosing instance of type Alerts (egxnew A () where x is an instance of alerts).

Alertas.java

public static void alertaHorarios(final Context context, final PerfilObj perfil)  {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = inflater.inflate(R.layout.alerta, null);       
    builder.setView(v);

    sonidos(context);

    cargarAlerta(context,
            v, 
            R.drawable.iconodescargar, 
            "Descargar datos", 
            "¿Desea descargar de internet los datos (horarios y tarifas) de " +
                    "<font color='" + 
                    context.getResources().getColor(Modulo.cargarColorAplicacion(context, perfil.getColor())) + "'><b>" + 
                    context.getResources().getString(R.string.app_name) + "</b></font>?",
            perfil.getColor());

    builder.setPositiveButton("Descargar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

            Modulo.reproducirSonidos(context, perfil.getSonidos(), sound, sonidoBoton);

            tareaAsincronaInicio task = new tareaAsincronaInicio(context, perfil);
            task.execute();

        }});
    builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

            Modulo.reproducirSonidos(context, perfil.getSonidos(), sound, sonidoBoton);
        }
    });      

    AlertDialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();

    Button b = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

    if(b != null) {
            b.setBackgroundResource(COLOR_FONDO_BOTONES);
            b.setTextSize(TAMANO_FUENTE_BOTONES);
            b.setTypeface(Modulo.fontPrincipal(context));
            b.setTextColor(context.getResources().getColorStateList(COLOR_FUENTE_BOTONES));
    }

    Button b2 = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    if(b2 != null) {
        b2.setBackgroundResource(COLOR_FONDO_BOTONES);
        b2.setTextSize(TAMANO_FUENTE_BOTONES);
        b2.setTypeface(Modulo.fontPrincipal(context));
        b2.setTextColor(context.getResources().getColorStateList(COLOR_FUENTE_BOTONES));
    }
}

private class tareaAsincronaInicio extends AsyncTask<Void, Integer, Boolean> {

            private Context mContext;
            private PerfilObj perfilObj;

            public tareaAsincronaInicio (Context context, PerfilObj perfil){
                 mContext = context;
                 perfilObj = perfil;
            }

...
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3580822
  • 31
  • 1
  • 1
  • 7

2 Answers2

0

If the AsyncTask code is written inside the Fragment/Activity as an inner class it is better to use YourActivityName.this instead of the context as Raghunandan commented.

If the code is is written outside the Activity/Fragment you can either use the BaseApplicition context cause if you use the Activity context might leak the activity. This can happen when the activity close's before the task is finished.

Aegis
  • 5,761
  • 2
  • 33
  • 42
0

Just use YourActivityName.this instead of passing the context to the AsyncTask.

Heisenberg
  • 656
  • 7
  • 10