protected String doInBackground(String... params) {
int progress = 0;
publishProgress(progress += 10);
String[] link_list = new String[100];
Bitmap bmp ;
Document xmlDoc = Jsoup.parse(url, 3000);
Elements title = xmlDoc.select("div[class=meta]");
title_lenth = title.size();
for (int i = 0; i < title_lenth; i++) {
link_list[count] = title.get(i).text();
try{
JSONObject JObj_link ;
JObj_link = new JSONObject(link_list[count]);
link_list[count] = JObj_link.getString("ou");
Log.e("ou Content", link_list[count]);
}catch (Exception e){
Log.e("ou Content", e.toString());
}
System.out.print(titlelist[count]);
count ++ ;
}
setBmp(link_list);
publishProgress(progress += 15);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(progress = 100);
return title_word;
}
我删除了一些不重要的代码
我想做
- &GT; setBmp(link_list);
link_list是一个字符串数组内容网址(http://xxx.xx.jpg)
setbmp可以下载pics并设置imageview
现在有错误消息
Logcat:
03-10 09:32:23.461 16218-16493/com.takepickpicturedemo E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.takepickpicturedemo, PID: 16218
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:942)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081)
at android.view.View.invalidateInternal(View.java:12713)
at android.view.View.invalidate(View.java:12677)
at android.view.View.invalidate(View.java:12661)
at android.widget.AbsListView.resetList(AbsListView.java:1996)
at android.widget.GridView.setAdapter(GridView.java:194)
at com.takepickpicturedemo.MainActivity.setBmp(MainActivity.java:741)
at com.takepickpicturedemo.MainActivity$GetPredict.doInBackground(MainActivity.java:582)
at com.takepickpicturedemo.MainActivity$GetPredict.doInBackground(MainActivity.java:497)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
修改
所以..
我该如何下载&amp;改变UI
答案 0 :(得分:1)
发生错误是因为setBmp()
以某种方式影响了视图,这在除UI线程之外的其他线程上是不允许的。用runOnUiThread()
包装它会有所帮助。替换
setBmp(link_list);
由:
runOnUiThread(new Runnable() {
@Override
public void run() {
setBmp(link_list);
}
});
为了使其有效,您还应该在创作时进行link_list
最终确认:
final String[] link_list = new String[100]; // "final" added
答案 1 :(得分:1)
如我所见,setBmp()
方法调用UI更新。 UI更新应该在UI线程中进行。您应该知道doInBackground
出现在后台线程中,不是 UI线程。
不要在doInBackground
方法中调用它,而是尝试从setBmp()
的{{1}}方法调用onPostExecute
。
注意:AsyncTask
方法在UI线程中执行。
答案 2 :(得分:-2)
这是 setBmp(link_list); 方法。可能是这个方法尝试显示图像。 **doInBackground**
方法未创建视图。
由于