确保在Ui线程中执行更新进度操作,可以使用Handler或AsyncTask的onProgressUpdate方法。例如:
class MyAsyncTask extends AsyncTask
检查进度对话框是否正确初始化。例如:
ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMax(100); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show();
Handler handler = new Handler(); new Thread(() -> { for (int i = 0; i <= 100; i++) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } final int progress = i; handler.post(() -> { progressDialog.setProgress(progress); }); } progressDialog.dismiss(); }).start();
检查进度对话框的样式是否正确设置,例如:
ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Loading..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.show();
new Thread(() -> { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } progressDialog.dismiss(); }).start();