1

我想在我的视图加载之前显示一个进度对话框。首先,我在 onCreate() 中编写了代码,但在这种情况下不会出现对话框。所以我在 onResume() 中写了它,但在这种情况下,即使在视图加载后它也不会消失。谁能告诉这里出了什么问题?

              protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    dialog = ProgressDialog.show(this, "", "Please wait...", true); 
    //dialog.cancel();
    new Thread() 
    {
      public void run() 
      {

         try
           {

            sleep(1500);

      // do the background process or any work that takes time to see progress dialog

           }  
        catch (Exception e)
        {
            Log.e("tag",e.getMessage());
        }
    // dismiss the progressdialog   
     dialog.dismiss();
     }
    }.start();
    citySelected.setText(fetchCity);
    spinner.setSelection(getBG);
}
4

3 回答 3

1

您无法从其他线程更新 UI(在主 UIthread 中)。如果要在后台运行任何查询,可以使用 AsyncTask。

在 onPreExecute 方法中,显示对话框和 onPostExecute 可以关闭对话框。

如果要使用 Thread,则使用处理程序更新 UI。

使用异步任务

public class MyAsyncTask extends AsyncTask<String, Void, String> {
    ProgressDialog dialog = new ProgressDialog(ActivityName.this);
    @Override
    protected void onPreExecute() {
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        dialog.dismiss();
        super.onPostExecute(result);
    }

}

在 Activity 的 onCreate 方法中,

MyAsyncTask task = new MyAsyncTask();
    task.execute();
于 2012-06-25T08:08:48.010 回答
0

您可以使用异步任务。它比线程好

private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);
    protected void onPreExecute() {
         this.dialog.setMessage("Please wait");
         this.dialog.show();

    }
    protected Boolean doInBackground(final String... args) {
        try {
                downloadFile(b.getString("URL"));
                return true;
        } catch (Exception e) {
                Log.e("tag", "error", e);
                return false;
        }
    }
    @Override
    protected void onPostExecute(final Boolean success) {

         if (dialog.isShowing()) {
         dialog.dismiss();
         }
    }
}
于 2012-06-25T08:06:08.060 回答
0

更好地使用Asynctask......但如果你仍然想要相同或想要know the solution only那么可以尝试

new Thread() 
    {
      public void run() 
      {

         try
           {

            sleep(1500);

      // do the background process or any work that takes time to see progress dialog

           }  
        catch (Exception e)
        {
            Log.e("tag",e.getMessage());
        }




  YourActivity.this.runOnUIThread(new Runnable(){
                         @Override
                         public void run(){
                             // dismiss the progressdialog   
                              dialog.dismiss();
                        });





     }
    }.start();
于 2012-06-25T08:13:04.677 回答