在使用AsyncTask
的时候,收到了如下的错误信息:
1 2 |
ERROR/AndroidRuntime(1936): FATAL EXCEPTION: main ERROR/AndroidRuntime(1936): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once) |
查了一下文档,才了解到AsyncTask
不可以复用。The task can be executed only once (an exception will be thrown if a second execution is attempted.)
AsyncTask文档链接AsyncTask。关键信息摘抄如下:
Threading rules
There are a few threading rules that must be followed for this class to work properly:
- The AsyncTask class must be loaded on the UI thread. This is done automatically as of
JELLY_BEAN
. - The task instance must be created on the UI thread.
execute(Params...)
must be invoked on the UI thread.- Do not call
onPreExecute()
,onPostExecute(Result)
,doInBackground(Params...)
,onProgressUpdate(Progress...)
manually. - The task can be executed only once (an exception will be thrown if a second execution is attempted.)