我在 doInBackround 有一个异步任务,我做了各种各样的事情,在各个部分之后我运行了
onProgressUpdate("You proceeded a bit further 1/5");
这多次工作正常,直到(仍然全部在 doInBackground 中)我有
HttpSessionToken = (HttpURLConnection)new URL("http://myserver").openConnection();
HttpSessionToken.setRequestMethod("GET");
HttpSessionToken.setRequestProperty("Accept", "application/json");
onProgressUpdate("Still everything is working fine");
int returnCode = HttpSessionToken.getResponseCode();
onProgressUpdate("This onProgressUpdate crashes!");
所以最后的onProgressUpdate崩溃了,但是为什么会崩溃呢? getResponseCode() 是否切换了我正在运行的线程?
最佳答案
你不能直接调用onProgressUpdate
,你必须调用publishProgress
并让AsynTask框架来处理要调用的onProgressUpdate
返回 UI 线程。
关于Android异步任务: Only the original thread that created a view hierarchy can touch its views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509183/