【发布时间】: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