我希望活动获得一些数据时出现进度对话框。我使用了Async任务,但是没有显示 我已经尝试了先前堆栈溢出问题的所有答案,但似乎没有一个适合我
private class BackgroundSync extends AsyncTask<Void,Void,Void> {
ProgressDialog progress = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
progress.setMessage("Loading");
progress.setTitle("Loading");
progress.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
/get data
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progress.dismiss();
super.onPostExecute();
}
}
我也尝试过使用
progress = ProgressDialog.show(MainActivity.this,"Loading","Loading");
作为一个堆栈溢出答案的建议,但它仍然没有显示
我也使用了下面的代码,但是这次ProgressDialog
并没有消失
private class BackgroundSync extends AsyncTask<Void,Void,Void> {
ProgressDialog progress
@Override
protected void onPreExecute() {
progress = new ProgressDialog(MainActivity.this);
progress.show(MainActivity.this,"Loading","Loading");
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
SyncEvents();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progress.dismiss();
}
}`
我该怎么办?这个ProgressDialog
问题已经花费了两个多小时来解决
答案 0 :(得分:0)
使用此代码
private class BackgroundSync extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
public BackgroundSync(MyMainActivity activity) {
dialog = new ProgressDialog(activity);
}
@Override
protected void onPreExecute() {
dialog.setMessage("Loading");
dialog.setTitle("Loading");
dialog.show();
}
@Override
protected Void doInBackground(Void... args) {
// do background work here
return null;
}
@Override
protected void onPostExecute(Void result) {
// do UI work here
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}