Android 错误编程对话框
我正在尝试将progressDialog转换为asynctask,但当我调用上下文“mainactivity.this”java show this message“在范围内无法访问mainactivity类型的封闭实例”时 看看代码Android 错误编程对话框,android,syntax-error,progressdialog,Android,Syntax Error,Progressdialog,我正在尝试将progressDialog转换为asynctask,但当我调用上下文“mainactivity.this”java show this message“在范围内无法访问mainactivity类型的封闭实例”时 看看代码 static class Tarea1 extends AsyncTask<ListView, String, CityAdapter>{ Context contexto; ListView list; InputStrea
static class Tarea1 extends AsyncTask<ListView, String, CityAdapter>{
Context contexto;
ListView list;
InputStream is;
ArrayList<City> cities = new ArrayList<City>();
public void cargarContenido(Context contexto){
this.contexto = contexto;
}
protected void onPreExecute(){
super.onPreExecute();
ProgressDialog pDialog = new ProgressDialog(MainActivity.this);//here is the error
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
静态类Tarea1扩展了异步任务{
语境;
列表视图列表;
输入流为;
ArrayList cities=新的ArrayList();
公共无效cargarContenido(上下文上下文){
this.contexto=contexto;
}
受保护的void onPreExecute(){
super.onPreExecute();
ProgressDialog pDialog=新建ProgressDialog(MainActivity.this);//下面是错误
pDialog.setMessage(“正在加载产品。请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
这是因为您的Tarea1
类被定义为静态类。当内部类是静态类时,您不能引用外部类中的对象
ZouZou的评论是正确的,因为您要将上下文传递到构造函数中,所以应该使用this.context
。只需确保您传递的上下文是活动上下文
否则,您可以删除
static
限定符。这是一个品味问题,但我更喜欢使用静态内部类,就像您必须使它们与外部类更加解耦一样。这样,如果/当您将其移动到它自己的文件中时,您不必将它们分开。既然您为异步任务提供了上下文,为什么不这样做呢ingProgressDialog pDialog=newprogressdialog(contexto);
?好的,美国this.getAppContext();///新建ProgressDialog(this.getAppContext());