可运行的发布成功,但无法运行 [英] Runnable is posted successfully but not run

查看:126
本文介绍了可运行的发布成功,但无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在现有的Andr​​oid项目我遇到了下面这段code(在这里我插调试屑)

In an existing Android project I've encountered the following piece of code (where I inserted the debugging litter)

ImageView img = null;

public void onCreate(...) {

    img = (ImageView)findViewById(R.id.image);

    new Thread() {
        public void run() {
            final Bitmap bmp = BitmapFactory.decodeFile("/sdcard/someImage.jpg");
            System.out.println("bitmap: "+bmp.toString()+" img: "+img.toString());
            if ( !img.post(new Runnable() {
                public void run() {
                    System.out.println("setting bitmap...");
                    img.setImageBitmap(bmp);
                    System.out.println("bitmap set.");
                }
            }) ) System.out.println("Runnable won't run!");
            System.out.println("runnable posted");
        }
    }.start();

新到Android的发展,并具有Google搜索的时候,我明白,这是做的东西,而不会阻塞主(UI)线程,同时还设置图像在UI线程解码后的方式。 (我已经通过登录<$ C $核实href="http://developer.android.com/resources/articles/painless-threading.html"> C> Thread.currentThread()的getName()在不同的地方)

New to Android development, and having googled around, I understand that this is the way to do stuff without blocking the main (UI) thread, while still setting the image on the UI thread after decoding. (at least according to android-developers) (which I have verified by logging Thread.currentThread().getName() at various places)

现在有时的图像只是不露面,和stdout只是说

Now sometimes the image just doesn't show up, and stdout only says

I/System.out( 8066): bitmap: android.graphics.Bitmap@432f3ee8 img: android.widget.ImageView@4339d698
I/System.out( 8066): runnable posted

有没有一丝的可运行的邮件。所以appearantly了Runnable没有的run(),虽然 img.post()返回真正的。拉动ImageView的在的onCreate()并宣布它最后并没有帮助。

with not a trace of the messages from the Runnable. So appearantly the Runnable doesn't run(), although img.post() returns true. Pulling the ImageView in onCreate() and declaring it final doesn't help.

我无言以对。简单地直接设置位,而阻塞UI线程,不解决的事情,但我希望把事情做好。有谁知道这是怎么回事吗?

I'm clueless. Simply setting the bitmap directly, while blocking the UI thread, does fix things, but I want to get things right. Does anybody understand what's going on here?

(PS。这是所有在Android 1.6手机和Android-3 SDK观察)

(ps. this was all observed on an Android 1.6 phone and android-3 sdk)

推荐答案

如果你看一下文档的<一个href="http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29"><$c$c>View.post有一些相关的信息:

If you look at the docs for View.post there's some relevant info:

此方法可以从UI线程之外调用,只有当   这种观点是连接到一个窗口。

This method can be invoked from outside of the UI thread only when this View is attached to a window.

既然你这样做是在的onCreate ,很可能有时你的查看将不会被连接到但该窗口。您可以通过覆盖<一个验证这一点href="http://developer.android.com/reference/android/view/View.html#onAttachedToWindow%28%29"><$c$c>onAttachedToWindow并把一些在日志中,也当您发布日志。你会看到,当出现故障后,才 onAttachedToWindow 后调用发生。

Since you're doing this in onCreate, it is likely that sometimes your View will not be attached to the window yet. You can verify this by overriding onAttachedToWindow and putting something in the logs and also logging when you post. You'll see that when the post fails, the post call happens before onAttachedToWindow.

由于其他人所说的,你可以使用<一个href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29"><$c$c>Activity.runOnUiThread或提供自己的处理程序。但是,如果您想直接从查看本身做到这一点,你可以简单地获得查看的处理程序:

As the others have mentioned, you can use Activity.runOnUiThread or provide your own handler. However, if you want to do it directly from the View itself, you can simply get the View's handler:

view.getHandler().post(...);

如果您有包含某种背景加载自定义视图这是非常有用的。还有不必创建一个新的独立的处理程序的额外的奖励。

This is especially useful if you have a custom view that includes some sort of background loading. There's also the added bonus of not having to create a new separate handler.

这篇关于可运行的发布成功,但无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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