在android中停止asyntask线程

时间:2012-08-09 14:50:01

标签: android android-asynctask threadpool

我正在扩展AsynTask以更新屏幕上的图像。图像通过套接字连续从输入流更新。 当我按下后退按钮时,我从asyn任务doinbackground()出来。从doinbackground()来之后,我正在关闭输入流和套接字。

我在哪里获得以下异常

08-09 20:19:24.410: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-09 20:19:24.420: W/System.err(32597):    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:448)
08-09 20:19:24.420: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-09 20:19:24.420: W/System.err(32597):    at com.einstructions.interwriteworkspace.WorkSpaceActivity$GetImageStreamFromServer.doInBackground(WorkSpaceActivity.java:618)
08-09 20:19:24.430: W/System.err(32597):    at android.os.AsyncTask$2.call(AsyncTask.java:252)
08-09 20:19:24.450: W/System.err(32597):    at java.lang.Thread.run(Thread.java:1020)
08-09 20:19:24.450: W/System.err(32597):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-09 20:19:24.470: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.480: W/System.err(32597):    at com.einstructions.interwriteworkspace.WorkSpaceActivity$GetImageStreamFromServer.doInBackground(WorkSpaceActivity.java:1)
08-09 20:19:24.480: W/System.err(32597):    at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
08-09 20:19:24.480: W/System.err(32597):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-09 20:19:24.490: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-09 20:19:24.490: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.490: W/System.err(32597):    at com.einstructions.interwriteworkspace.WorkSpaceActivity$GetImageStreamFromServer.doInBackground(WorkSpaceActivity.java:618)
08-09 20:19:24.500: W/System.err(32597):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-09 20:19:24.500: W/System.err(32597):    at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
08-09 20:19:24.500: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.500: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-09 20:19:24.510: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.510: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.510: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.520: W/System.err(32597):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-09 20:19:24.520: W/System.err(32597):    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:448)
08-09 20:19:24.520: W/System.err(32597):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)

请帮助我

此致 微型。

1 个答案:

答案 0 :(得分:0)

暂停/停止活动时,您必须取消GetImageStreamFromServer任务。 在onPause / onStop中使用,

getImageStreamFromServerTask.cancel(true);