【发布时间】:2013-09-03 13:40:06
【问题描述】:
AsyncTask 有问题。
我通过调用 webService 来更新对象列表。在此期间,我想显示ProgressDialog。然后,当列表完全更新时,我想要关闭对话框和 AsyncTask。
问题在于 UIThread 没有等待 AsyncTask 完成。我看到get() 方法可以为我工作,但使用它,UIThread 只是被阻止,对话框只是不显示。
我想知道如何在显示 ProgressDialog 的同时等待任务完成。
这是我的 AsyncTask 类:
public class MyAsyncTask extends AsyncTask<Integer, Void, Boolean>{
private ProgressDialog pd;
private String m_sShelfCode;
private String m_sFamiliyCode;
public MyAsyncTask(String shelfCode, String familyCode) {
this.m_sShelfCode = shelfCode;
this.m_sFamiliyCode = familyCode;
}
@Override
protected void onPreExecute() {
AppManager app = AppManager.getInstance();
LayoutInflater inflater = (LayoutInflater)app.m_AppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout rl = (RelativeLayout)inflater.inflate(R.layout.progress_dialog, null, false);
pd = new ProgressDialog(app.m_AppContext);
pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
pd.setCancelable(false);
pd.show();
pd.setContentView(rl);
}
@Override
protected Boolean doInBackground(Integer... params) {
Log.d("DO IN BACKGROUND","param[0]="+params[0]);
switch (params[0]) {
case AppManager.SHELF_LIST:
//Calling WebService
SoapShelf s = new SoapShelf();
try {
ArticleListFragment.m_ShelfList = s.getShelfList();
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (XmlPullParserException e) {
e.printStackTrace();
return false;
}
break;
case AppManager.FAMILY_LIST:
// set family list
break;
case AppManager.ARTICLE_LIST:
// set article list
break;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
pd.dismiss();
if (result == false)
;// error dialog
}
}
我在这里在我的 ArticleListFragment 中调用它
public void getItemList(int listType) {
switch (listType) {
case AppManager.SHELF_LIST:
new MyAsyncTask(null, null).execute(new Integer[]{AppManager.SHELF_LIST});
提前致谢
【问题讨论】:
-
也许在显示之前设置对话框的内容视图会有所帮助
-
不,它会引发异常。它需要是这样的。该对话框有效,我在我的应用程序的其他地方使用它。
-
抛出什么样的异常
-
android.util.AndroidRuntimeException: requestFeature() must be called before adding content -
你能发布你的完整代码和 logcat 吗,我猜你没有在 setContentView() 之前调用 requestFeature()。
标签: java android android-asynctask progressdialog