我使用asyncTask来发出http请求,因此我使用了进度条来指示该线程中的ongoings进度。在调用onPostexecute()
方法之前,一切正常,我在Logcat
中显示了响应字符串,但我的进度条没有退出。
有类似的问题,但我没有得到问题所需的解决方案。 请帮忙。
这是我的代码:
class RequestTask extends AsyncTask<String, Integer, String> {
@SuppressWarnings("static-access")
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
prgDialog=new ProgressDialog(getActivity());
prgDialog.setCancelable(true);
prgDialog.show(getActivity(), null, "Loading...");
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("result", result);
prgDialog.dismiss();
// Do anything with response..
}
}
答案 0 :(得分:5)
您没有在您认为(或想要)的dismiss()
个实例上调用ProgressDialog
。您创建了ProgressDialog
并将其分配给prgDialog
引用,仅用于调用:
prgDialog.show(getActivity(), null, "Loading...");
这将创建一个新 ProgressDialog
实例,您不会因为prgDialog
指向另一个未显示ProgressDialog
的人而被解雇。尝试:
protected void onPreExecute() {
super.onPreExecute();
prgDialog = ProgressDialog.show(getActivity(), null, "Loading...");
}
这就是你应该在类上调用静态方法而不是该类的某个实例的原因。