我是 android 的新手,我试图从服务器获取一些数据并将其打印到我的设备中。我意识到使用 android 4.x 我们无法做到这一点,因为 UI 线程被阻塞了。我进行了研究,发现了 AsyncTask。有人可以对我的代码提供一些帮助吗?有什么遗漏吗?谢谢
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread().execute();
openDialog(getCurrentFocus());
}
异步任务类:
private class Thread extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//Here i make a HTTP request.
return null;
}
}
日志猫:
04-22 22:43:10.808: E/Trace(12987): error opening trace file: No such file or directory (2)
04-22 22:43:11.098: E/AndroidRuntime(12987): FATAL EXCEPTION: AsyncTask #1
04-22 22:43:11.098: E/AndroidRuntime(12987): java.lang.RuntimeException: An error occured while executing doInBackground()
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.lang.Thread.run(Thread.java:856)
04-22 22:43:11.098: E/AndroidRuntime(12987): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.os.Handler.<init>(Handler.java:197)
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.os.Handler.<init>(Handler.java:111)
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.widget.Toast$TN.<init>(Toast.java:324)
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.widget.Toast.<init>(Toast.java:91)
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.widget.Toast.makeText(Toast.java:238)
04-22 22:43:11.098: E/AndroidRuntime(12987): at com.application.uprm_map.MainActivity.startApplication(MainActivity.java:72)
04-22 22:43:11.098: E/AndroidRuntime(12987): at com.application.uprm_map.MainActivity.access$0(MainActivity.java:67)
04-22 22:43:11.098: E/AndroidRuntime(12987): at com.application.uprm_map.MainActivity$Thread.doInBackground(MainActivity.java:439)
04-22 22:43:11.098: E/AndroidRuntime(12987): at com.application.uprm_map.MainActivity$Thread.doInBackground(MainActivity.java:1)
04-22 22:43:11.098: E/AndroidRuntime(12987): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-22 22:43:11.098: E/AndroidRuntime(12987): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-22 22:43:11.098: E/AndroidRuntime(12987): ... 3 more
04-22 22:43:11.413: E/WindowManager(12987): Activity com.application.uprm_map.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{421b4b58 V.E..... R.....I. 0,0-720,412} that was originally added here
04-22 22:43:11.413: E/WindowManager(12987): android.view.WindowLeaked: Activity com.application.uprm_map.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{421b4b58 V.E..... R.....I. 0,0-720,412} that was originally added here
04-22 22:43:11.413: E/WindowManager(12987): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
04-22 22:43:11.413: E/WindowManager(12987): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)