简体   繁体   中英

Android doesn't show ProgressDialog

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:

  • The catch of the try/catch showed in the code
  • The onPostExecute method of AsyncTask.

Also I'm using too few data for testing, so it comes up and down too fast.

Use AsyncTask for Background processing and Updating Progressing in Foreground. I think that best suited your task.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM