Android 在asynctask中正确设置进度对话框
我目前正试图在对话框的OnclickListener上显示进度对话框,因为从服务器获取我的项目花费的时间太长。 我使用这里建议的异步任务()和本文()来显示进度对话框。进度对话框会显示出来,但是当代码转到do background“Looper未设置”时会返回异常。当我设置活套时,什么也没发生 我不确定在这个阶段我做错了什么Android 在asynctask中正确设置进度对话框,android,android-asynctask,progressdialog,dialog,Android,Android Asynctask,Progressdialog,Dialog,我目前正试图在对话框的OnclickListener上显示进度对话框,因为从服务器获取我的项目花费的时间太长。 我使用这里建议的异步任务()和本文()来显示进度对话框。进度对话框会显示出来,但是当代码转到do background“Looper未设置”时会返回异常。当我设置活套时,什么也没发生 我不确定在这个阶段我做错了什么 public void firstMethod() { final CustomObj obj = getCustomObj();//not imp
public void firstMethod()
{
final CustomObj obj = getCustomObj();//not imp
Messages.getInstance().showAlert(MainActivity.this, "message", false, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
gotoAnotherPge(obj);
}
});
}
public void gotoAnotherPge(final CustomObject obj)
{
final ProgressDialog pd = new ProgressDialog(MainActivity.this);
new AsyncTask<Object, Object, Boolean>()
{
protected void onPreExecute()
{
pd.setMessage(String.format(Statics.getText(MainActivity.this, R.raw.dictionary, "subscriptions_loading_unsubscribing")));
pd.show();
}
protected Boolean doInBackground(Object... params)
{
try{
Looper.prepare();
final LocalPopulator lp = new LocalPopulator(MainActivity.this, 0)
{
@Override
public void populate()
{
List<Serializable> items = Arrays.asList(getItemHere(obj));
List<Serializable> listItems = new ArrayList<Serializable>();
listItems.addAll(items);
Serializable[] sItems = listItems.toArray(new Serializable[menuItems.size()]);
result = sItems;
}
};
showNextPage(true, 1, 0, lp);
Looper.loop();
}catch (Exception e){
Log.e("tag", e.getMessage());
/*
* The task failed
*/
return false;
}
return true;
}
protected void onPostExecute(Boolean result)
{
pd.dismiss();
}
};
MainActivity.this.runOnUiThread (new Runnable()
{
@Override
public void run()
{
// dismiss the progressdialog
pd.dismiss();
}
});
}
public-void-firstMethod()
{
final CustomObj obj=getCustomObj();//不是imp
Messages.getInstance().showAlert(MainActivity.this,“message”,false,新建DialogInterface.OnClickListener())
{
@凌驾
public void onClick(DialogInterface dialog,int which)
{
dialog.dismise();
}
},新建DialogInterface.OnClickListener()
{
@凌驾
public void onClick(DialogInterface dialog,int which)
{
Gotoanotherpage(obj);
}
});
}
公共无效转到其他公司(最终自定义对象obj)
{
final ProgressDialog pd=新建ProgressDialog(MainActivity.this);
新建异步任务()
{
受保护的void onPreExecute()
{
setMessage(String.format(Statics.getText(MainActivity.this,R.raw.dictionary,“订阅\加载\取消订阅”));
pd.show();
}
受保护的布尔doInBackground(对象…参数)
{
试一试{
Looper.prepare();
最终LocalPopulator lp=新的LocalPopulator(MainActivity.this,0)
{
@凌驾
公共空白填充()
{
List items=Arrays.asList(getItemHere(obj));
List listItems=new ArrayList();
listItems.addAll(项目);
Serializable[]sItems=listItems.toArray(新的Serializable[menuItems.size());
结果=站点;
}
};
显示下一页(正确,1,0,lp);
loop.loop();
}捕获(例外e){
Log.e(“tag”,e.getMessage());
/*
*任务失败了
*/
返回false;
}
返回true;
}
受保护的void onPostExecute(布尔结果)
{
pd.解散();
}
};
MainActivity.this.runOnUiThread(新的Runnable()
{
@凌驾
公开募捐
{
//关闭progressdialog
pd.解散();
}
});
}
对于微调器(进度对话框)为什么不显示,我找不到任何答案。它被放在后台,让populater先完成,然后再显示出来。我做了一项工作来解决这个问题,而不是在主线程上使用populater,我创建了服务populater,它作为后台任务运行。现在,我在加载新屏幕后显示进度对话框,然后加载数据。