Android AsyncTask中的ProgressBar-在onPostExecute方法之后显示
我通过包含AsyncTask的外部类从服务器检索数据:Android AsyncTask中的ProgressBar-在onPostExecute方法之后显示,android,android-asynctask,progress-bar,Android,Android Asynctask,Progress Bar,我通过包含AsyncTask的外部类从服务器检索数据: public class GetTask extends AsyncTask<String, Void, JSONObject> { private Context context; ProgressDialog dialog; public GetTask(Context cxt) { context = cxt; dialog = new ProgressDialo
public class GetTask extends AsyncTask<String, Void, JSONObject> {
private Context context;
ProgressDialog dialog;
public GetTask(Context cxt) {
context = cxt;
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
dialog.setTitle("Load...");
dialog.setMessage("Data...");
dialog.show();
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... url) {
// code for retreive data
return jArray;
}
protected void onPostExecute(JSONObject object) {
dialog.dismiss();
super.onPostExecute(object);
}
}
成功检索我的数据,但在super.onPostExecute(对象)之后显示ProgressDialog;方法,为什么
在以下步骤后显示“附加说明”对话框:
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
关于内部Looper.class
对不起,我的英语不好 我找到了解决方案,需要使用回调,而不是使用.get()方法。我把我的任务称为:
callTask(linkODeatails, obj);
呼叫任务:
void callTask(String link, String object){
task.new GetTask(this).execute(link + object);
}
我创建界面:
public interface AsyncTaskCompleteListener {
public void onTaskComplete(JSONObject result);
}
并在我的任务中添加:
private Activity activity;
private AsyncTaskCompleteListener callback;
public GetTask(Activity act){
this.activity = act;
this.callback = (AsyncTaskCompleteListener)act;
}
他们呼吁:
callback.onTaskComplete(result, object);
您取消任务了吗?您的意思是-dialog.setCancelable(true)?不,我是说任务。取消(对)。您确定正在调用onPostExecute吗?我调试我的应用程序,是的,在onPostExecute调试器切换到Looper.class并在code
Binder.clearCallingIdentity()后,正在调用onPostExecute;最终长标识=活页夹。clearCallingIdentity()代码>嗨,删除后问题解决。get();方法调用任务过程。。请帮忙。
callback.onTaskComplete(result, object);