在后台线程充气视图 [英] Inflate a view in a background thread

查看:114
本文介绍了在后台线程充气视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很简单的问题:

I have a very simple question:

时还是不可能的充气视图(而不是将它添加到布局)在后台线程(例如:在 doInBackground 的AsyncTask )?

Is or is it not possible to inflate a view (not add it to layout) in a background thread (ex: in the doInBackground of an AsyncTask)?

我知道这是可能的,因为我已经实现了我的应用程序大多数活动这样的,从来没有问题,直到我有一个Galaxy S的这个问题:<一href="http://stackoverflow.com/questions/6682730/android-android-view-inflateexception-binary-xml-file-line-13-error-inflating/">Android: android.view.InflateException:二进制XML文件中的行#13:错误充气类&lt;未知&GT;在三星Galaxy S

I know that it is possible, because I have implemented most Activities in my application this way and never had a problem, until I had this problem on a Galaxy S: Android: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown> in SAMSUNG Galaxy S

我已经告诉我的的不充气浏览在后台线程,但具体是什么原因?为什么我的aproach工作在大多数设备,但不是在Galaxy S的?

I've been told that I should not inflate Views in background threads, but what are the specific reasons and why does my aproach work in most devices but not in Galaxy S?

推荐答案

LayoutInflater 并没有做出什么线程运行在任何假设。而没有提及关于这个的文档。另外它的code接缝线程无关。

The LayoutInflater does not make any assumptions about what thread it runs on. And nothing is mentioned about this in its documentation. Also its code seams to be thread-agnostic.

在另一方面,是由 LayoutInflater 创建视图可能实例处理程序 S在它们的构造。那么,他们可能不应该这样做,但没有要求他们不要创建/使用处理程序 S在它们的构造函数。

On the other hand, Views that are created by LayoutInflater might instantiate Handlers in their constructors. Well, they probably shouldn't do that, but there is no requirement for them to not create/use Handlers in their constructors.

我的猜测是,三星Galaxy S有一些修改,在其的EditText ,不知怎的,触发创作处理程序的(根据从 GestureDetector 您的其他问题,例如崩溃日志被实例化这反过来又创造了新的处理程序)。虽然默认实现不执行此操作。

My guess is that Samsung Galaxy S had some modifications in its EditText that somehow triggers creation of Handler (according to crash log from your other question instance of GestureDetector was instantiated which in turn created new Handler). While default implementation doesn't do this.

总之,我会说,因为对查看秒到不使用没有明确规定处理程序取值和尺蠖 S在它们的构造函数,你不能假设从非UI充气查看线程是安全的。

Overall, I'd say that because there is no explicit requirement for Views to not use Handlers and Loopers in their constructors you can't assume inflating Views from non-UI thread is safe.

您可以实际创建<一个href="http://developer.android.com/reference/android/os/HandlerThread.html"><$c$c>HandlerThread并尽量夸大查看的内线吧。但我要说,这是非常危险的,如三星Galaxy S例如认为假定在查看一生的主题将是活着的,并会使用处理所有消息的尺蠖。这可能会导致崩溃以后。

You can actually create HandlerThread and try inflating Views inside it. But I'd say this is very risky, as in Samsung Galaxy S example the view assumes that this thread will be alive during View lifetime and will process all messages using its Looper. Which might result in crash later on.

这篇关于在后台线程充气视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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