【问题标题】:Error in AsyncTask with parameters带参数的 AsyncTask 中的错误
【发布时间】:2014-04-29 10:16:16
【问题描述】:

我正在尝试创建一个AsyncTask,并将Context 作为onClick 的button 的Alert 的参数onClick。

但我得到了错误:

没有可访问类型的封闭实例警报。必须符合条件 分配带有警报类型的封闭实例(例如xnew A() 其中 x 是警报的一个实例)。

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;
            }

...
}

【问题讨论】:

  • 上下文在哪里初始化?
  • onClick 在一个方法中,它有上下文作为参数
  • 改用YourActivityName.this
  • 我有一个类 Alerts ,其中包含许多带有不同警报的方法。在 onClick 警报中,我调用了 AsyncTask
  • 你需要一个上下文。活动上下文。你需要通过它。

标签: android button android-asynctask onclick android-context


【解决方案1】:

如果 AsyncTask 代码作为内部类写在 Fragment/Activity 中,最好使用 YourActivityName.this 而不是 Raghunandan 评论的上下文。

如果代码是在 Activity/Fragment 之外编写的,您可以使用 BaseAppliction 上下文,因为如果使用 Activity 上下文可能会泄漏活动。当活动在任务完成之前关闭时,可能会发生这种情况。

【讨论】:

  • 我已经编辑了我的代码。 Alertas 类不是 Activity/Fragment
【解决方案2】:

只需使用 YourActivityName.this 而不是将上下文传递给 AsyncTask。

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多