我想要一个进度对话框,说“请稍候”,而我的代码下载并解析了一个 xml 文件,但进度对话框没有运行,它不知何故卡住了。我正在使用 AsyncTask,xml 文件的下载和解析是在后台线程中完成的,如果我删除进度对话框,代码就可以工作,但在这种情况下,您必须等待大约 4 秒才能单击动态创建 UI 的按钮。
private class ParseXML extends AsyncTask<Integer, Integer, Document>{
private Context context;
private Activity activity;
private ProgressDialog pd;
public ParseXML(Activity activity) {
this.activity = activity;
context = activity;
pd = new ProgressDialog(context);
}
public void onPreExecute(){
super.onPreExecute();
/* ProgressDialog pd=ProgressDialog.show(XML_PARSER.this,"","Please Wait");*/
}
@Override
protected Document doInBackground(Integer... params) {
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
Document dom1=null;
try {
//InputStream is=getResources().openRawResource(R.raw.options);
URL requestURL = new URL("my url");
URLConnection connection = requestURL.openConnection();
is = connection.getInputStream();
DocumentBuilder db = dbf.newDocumentBuilder();
dom1=db.parse(is);
Log.i(TAG,"parsing done");
}
catch(ParserConfigurationException pce){
pce.printStackTrace();
}
catch(SAXException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
ParseDocument(dom1);
return null;
}
@Override
public void onPostExecute(Document d){
if(pd!=null && pd.isShowing()){
pd.dismiss();
}
super.onPostExecute(d);
}
}