AsyncTask的使用Progressdialog [英] Asynctask with Progressdialog
问题描述
我使用的是AsyncTask的执行耗时的方法, doStuff()
,除其他外分配全局数据结构。调试器确认 doStuff()
被调用,但是当一个新的观点是在的AsyncTask年底得出,我得到一个空指针异常而访问全局数据结构。这里是code:
I'm using an Asynctask to execute a time-consuming method, doStuff()
, which among other things allocates global data structures. A debugger confirms that doStuff()
is called, but when a new view is drawn at the end of the Asynctask, I get a null pointer exception while accessing the global data structures. Here is the code:
public class MyTask extends AsyncTask<Void, Void, Void> {
protected ProgressDialog dialog;
public MyTask (Context context) {
dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Foo");
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
doStuff();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
我用新MyTask(本).execute()从多个活动执行的AsyncTask;
推荐答案
的AsyncTask
确实是一个围绕一个后台线程和处理器的便捷包装器。您正在执行code中的后台任务完成之前。要知道,当后台任务完成后,你需要把code要执行或以其他方式预示着 onPostExecute
您的主要活动(这将在UI运行/主线程)的任务就完成了。有关详细信息,请参见进程和线程指南的使用AsyncTask的栏目一>
AsyncTask
is a really a convenience wrapper around a background thread and a handler. You are executing code before the background task finishes. To know when the background task is finished, you need to put the code you want to execute or otherwise signal your main activity in onPostExecute
(which will run on the UI/main thread) that the task is complete. For more details, see the using AsyncTask section of the Processes and Threads guide.
这篇关于AsyncTask的使用Progressdialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!