执行AsyncTask的几次 [英] Execute AsyncTask several times

查看:147
本文介绍了执行AsyncTask的几次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我使用从AsyncTask的延伸的类和参数,它是的AsyncTask的一个实例。当我打电话 mInstanceOfAT.execute()一切都很好。 但是,应用程序崩溃时,我preSS的更新按钮,再次调用AsyncTask的(如果网络工作没有工作)。原因然后出现一个异常它说

In my Activity I use a class which extends from AsyncTask and a parameter which is an instance of that AsyncTask. When I call mInstanceOfAT.execute("") everything is fine. But the app crash when I press an update button which calls again the AsyncTask(In case the network job didnt work). Cause then appears an Exception which says

无法执行的任务:任务有   已经执行(一个任务可以是   只执行一次)

Cannot execute task: the task has already been executed (a task can be executed only once)

我已经打过电话取消(真)的Asyctask的实例,但它不工作的。唯一的解决办法,到目前为止它的创建Asyntask的新实例。那是正确的方法是什么?

I have tried calling cancel(true) for the instance of the Asyctask, but it doesnt work either. The only solution so far it's to create new instances of the Asyntask. Is that the correct way?

感谢。

推荐答案

的AsyncTask 情况下只能使用一次。

AsyncTask instances can only be used one time.

相反,只是打电话给你的任务,比如新MyAsyncTask()执行();

Instead, just call your task like new MyAsyncTask().execute("");

从AsyncTask的API文档:

From the AsyncTask API docs:

有必须遵循此类正常工作的几个线程的规则:

There are a few threading rules that must be followed for this class to work properly:

  • 任务实例必须在UI线程创建的。
  • 执行(参数...)必须在UI线程上被调用。
  • 请不要在preExecute(),onPostExecute(结果),doInBackground(参数...),onProgressUpdate(进步...)手动。
  • 打电话
  • 的任务可以只执行一次(如果是第二次执行尝试一个异常将被抛出。)

这篇关于执行AsyncTask的几次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆