在doInBackground改变UI线程的意见中的AsyncTask,CalledFromWrongThreadException并不总是抛出 [英] Altering UI thread's Views in AsyncTask in doInBackground, CalledFromWrongThreadException not always thrown

查看:284
本文介绍了在doInBackground改变UI线程的意见中的AsyncTask,CalledFromWrongThreadException并不总是抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code在AsycnTask:

@Override
protected Boolean doInBackground(View... params) {

try{

    Drawable drawPhoto = DataDatero.ImageDownload(taskPhotoName);
    ((ImageView)params[0]).setImageDrawable(drawPhoto);
    ((TextView)params[1]).setText(taskItemListText);
    ((TextView)params[2]).setTextColor(taskColore);
    ((TextView)params[2]).setText(taskItemStockText);
    [...]
}

抛出Call​​edFromWrongThreadException,描述了:

Throws a CalledFromWrongThreadException , describing that:

只有创建视图层次可以触摸原始线程的
  意见

Only the original thread that created a view hierarchy can touch its views

本已充裕的问题进行了讨论:<一href=\"http://stackoverflow.com/questions/7937399/android-view-viewrootcalledfromwrongthreadexception-only-the-original-thread-t\">example ,另一个<一个href=\"http://stackoverflow.com/questions/13063408/no-error-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-v\">example ;和所有得出同样的结论。

This has been discussed in plenty of questions: example , another example ; and all reach the same conclusion.

但是我不会只得到了异常..

我称之为的执行的从列表适配器getView,图像(PARAMS [0])在视图中,而抛出异常的文本进行更新。另外,如果我只留下了TextView的更新,文本被更新,不抛出任何异常。

I call that execute from a getView in a List adapter, and the images (params[0]) are updated in the view while the exception is thrown for the text. Also, if I leave only the textview updates , the text is updated and no exception is thrown.

如果我更新TextView的第一和后ImageView的,一些文本和图像被更新,而有的图像不(即imageDownload包括2-3秒的网络操作)​​

If I update the TextView first and the ImageView after, some text and images are updated while some images are not (that imageDownload includes a 2-3 sec network operation)

为什么他们中的一些更新,有些不是?

注:这只能通过SDK 4.0,V4支持测试,在API 16仿真。
我固定它,我没有接触doInBackground意见
第二个例子是类似的东西...是,如果是的onCreate没有完成该操作被验证?

推荐答案

我也碰到过类似的问题,并问了一个问题<一href=\"http://stackoverflow.com/questions/29918912/modifying-views-in-asynctask-doinbackground-does-not-always-throw-exception\">here (个体经营挖掘好位后回答)。

I have come across a similar issue and asked a question here (self answered after a good bit of digging).

从本质上讲它归结为是,相反的是大家都认为,你的可以的从的AsyncTask <$ C $修改UI元素C>的execute()的如果的这些观点都没有经过布局穿越了呢。这种异步发生在执行(活动生命周期方法/回调)的主流,因此,如果查看问题之前即将创建的execute()被调用时,你的可以的访问它们(意思,没有抛出异常,它仍然是当然非常糟糕的做法,并不可取)。因为的execute()发生在另一个线程,布局遍历(其中UI线程上运行)可以完成,而你的的execute()运行,这也解释了为什么只有某些观点可能会被修改等人抛出异常。这也解释了为什么只留下TextView的更新(和presumably删除的ImageView 更新)中的更新结果神奇工作压力太大。由于这是一个计时相关的问题,这取决于许多因素,除其他事项外多久可绘制drawPhoto = DataDatero.ImageDownload(taskPhotoName); 需要运行

Essentially what it boils down to is that, contrary to what everybody thinks, you can modify UI elements from an AsyncTask execute() if those views haven't gone through a layout traversal yet. This happens asynchronously to the main flow of execution (activity lifecycle methods/callbacks), so if the View in question is created shortly before execute() is called, you can access them (meaning, the exception isn't thrown, it's of course still really bad practice and not advisable). Because execute() happens on another thread, the layout traversals (which run on the UI thread) may finish while your execute() is running, which explains why only some views may be modified and others throw the exception. It also explains why "leaving only the textview updates" (and presumably removing the ImageView updates) results in those updates "magically" working too. Since this is a timing related issue, it depends on many factors, among other things how long Drawable drawPhoto = DataDatero.ImageDownload(taskPhotoName); takes to run.

PS:我意识到这是一个迟到的答案,但是我觉得这个可以先找到这个答案,不会有太多的帖子处理这样的问题,是有人用

PS: I realise this is a late answer, but I think this can be useful for somebody finding this answer first, there aren't many posts dealing with issues like this.

这篇关于在doInBackground改变UI线程的意见中的AsyncTask,CalledFromWrongThreadException并不总是抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆