我在AsyncTask中显示一个ProgressDialog,如下所示。
public class DataComm extends AsyncTask<String, Void, JSONObject> {
...
ProgressDialog pd = null;
...
protected void onPreExecute() {
pd = ProgressDialog.show(activity, activity.getResources().getText(R.string.wait_please) + "\u2026", "", true);
...
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
...
pd.dismiss();
}PD在实现AsyncTask的类中声明。我在onPreExecute中初始化它,使用创建的活动的上下文,并尝试在doInbackground的末尾调用pd.dismiss()。这是从多个活动中调用的,在某些情况下它可以工作,但在其他情况下,我会得到一个RuntimeException“将消息发送到死线程上的处理程序”。在失败的情况下,我知道我用来创建PD的活动仍然是活动的,所以我不知道哪个线程已经死了。
有问题的调用来自一个类,该类派生自一个基类,该基类用于更新从ArrayAdapter派生的类的列表。当ArrayAdapter需要填充选项列表时,将调用AsyncTask。在UI主线程之外的其他线程上会发生这种情况吗?我仍然不明白为什么它应该在onPostExecute被调用之前就死掉。
发布于 2014-08-11 02:24:04
异步任务doInbackground方法在后台的独立线程中运行,而不是主线程。请检查它可能是因为某些原因,这是死亡。
发布于 2014-08-11 02:53:23
我认为你应该首先为AsyncTask DataComm创建构造器,然后在构造器中初始化ProgressDialog,如下所示:
public class DataComm extends AsyncTask<String, Void, JSONObject> {
...
ProgressDialog pd;
...
public void DataComm(Context mContext)
{
pd = new ProgressDialog(mContext);
}
protected void onPreExecute() {
this.pd.setMessage(mContext.getResources().getText(R.string.wait_please) + "\u2026");
this.pd.show();
...
}
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
...
if (pd.isShowing()) {
pd.dismiss();
}
}发布于 2014-08-11 08:49:12
试试这个:
public class DataComm extends AsyncTask<String, Void, Void> {
ProgressDialog pd = null;
protected void onPreExecute() {
pd = ProgressDialog.show(activity, activity.getResources().getText(R.string.wait_please) + "\u2026", "", true);
}
protected void onProgressUpdate(Integer... progress) {
pd.setProgress(progress[0]);
}
protected void onPostExecute() {
super.onPostExecute(result);
...
activity.runOnUiThread(new Runnable() {
@Override
public void run(){
if (pd.isShowing()) {
pd.dismiss();
}
}
});
}
}https://stackoverflow.com/questions/25231670
复制相似问题