I'm trying to show a ProgressDialog when loading an Activity, but it doesn't show up.
Here is the method called on Activity onCreate
private void loadBuilding(String[] b) {
ProgressDialog pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(6);
pd.setTitle(R.string.loading);
pd.show();
LoadBuilding lb = new LoadBuilding();
lb.initialize(this, pd);
lb.execute(b);
try {
lb.get();
} catch (Exception e) {
e.printStackTrace();
}
pd.dismiss();
if (building == null)
showError();
}
The LoadBuilding is an AsyncTask in which I load the building and set the progress.
Thanks to all.
Problem was that the progressDialog.dismiss() must be in:
Also I'm using too few data for testing, so it comes up and down too fast.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Try to add your progressDismiss() in try and catch block
try
{
pg.dismiss();
}
Catch()
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.