我有一个以 AsyncTask 开头的活动,以检查与我的服务器 api 的连接。由于这不应该在 UI 线程中运行,因此我为此创建了 AsyncTask。
问题是当我启动 Asynctask 的后台进程时出现运行时异常。
我怎样才能避免这种情况?
从日志:
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): java.lang.RuntimeException: An error occured while executing doInBackground()
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at java.lang.Thread.run(Thread.java:1058)
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-12 14:18:56.476: ERROR/AndroidRuntime(1523): at android.os.Handler.<init>(Handler.java:121)