在 ImageSwitcher 中 setImageDrawable 和 setImageResource 时出现 NullPointerException [英] NullPointerException when setImageDrawable and setImageResource in an ImageSwitcher

查看:84
本文介绍了在 ImageSwitcher 中 setImageDrawable 和 setImageResource 时出现 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用资源填充 ImageSwitcher 时,我不断收到 NullPointerException 错误.在 onPreExecute() 期间,使用 AsyncTask 中的 WeakReference 调用它:

I keep getting NullPointerException errors when I try to populate an ImageSwitcher with a resource. It is called using a WeakReference from within an AsyncTask, during the onPreExecute():

if (imageSwitcherReference != null) {
                ImageSwitcher imageSwitcher = imageSwitcherReference.get();
                if (imageSwitcher != null) {
                    imageSwitcher.setImageResource(R.drawable.receta_nofoto);
                }
            }

这是前堆栈跟踪:

02-21 16:13:10.639: ERROR/AndroidRuntime(4274): java.lang.NullPointerException
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.widget.ImageSwitcher.setImageResource(ImageSwitcher.java:41)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at com.paravegetarianos.motores.DescargadorImagenes.onPreExecute(DescargadorImagenes.java:90)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.os.AsyncTask.execute(AsyncTask.java:391)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at com.paravegetarianos.FotosGaleria.seleccionarImagen(FotosGaleria.java:57)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at com.paravegetarianos.FotosGaleria.access$0(FotosGaleria.java:53)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at com.paravegetarianos.FotosGaleria$1.onItemSelected(FotosGaleria.java:43)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.widget.AdapterView.access$200(AdapterView.java:42)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.os.Handler.handleCallback(Handler.java:587)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.os.Looper.loop(Looper.java:123)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at java.lang.reflect.Method.invoke(Method.java:521)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274):     at dalvik.system.NativeStart.main(Native Method)

当尝试使用我刚刚创建的 BitmapDrawable 设置 Drawable 时,它​​也会变成 NullPointerException.

It also turns into a NullPointerException when trying to set the Drawable with a BitmapDrawable I created just a second before.

if (imageSwitcherReference != null) {
                ImageSwitcher imageSwitcher = imageSwitcherReference.get();
                if (imageSwitcher != null) {
                    BitmapDrawable bm = new BitmapDrawable(result);
                    imageSwitcher.setImageDrawable(bm);
                }
            }

其中 result 是在 onPostExecute() 步骤中捕获的位图.我调用 AsyncTask 的那一刻是在 onItemSelectedListener() 构建图库时:

Where result is a Bitmap caught in the onPostExecute() step. The moment I call the AsyncTask is when onItemSelectedListener(), when building a Gallery:

    galeria.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            seleccionarImagen(arg2);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            seleccionarImagen(0);
        }
    });

其中 galeria 是 Gallery,seleccionarImagen(int) 获取图像索引并调用 AsyncTask.

where galeria is the Gallery, and seleccionarImagen(int) gets the image index and calls the AsyncTask.

这是后堆栈跟踪(在评论错误行之后):

And here's the post stacktrace (after commenting the error line):

02-21 16:04:42.849: ERROR/AndroidRuntime(4201): FATAL EXCEPTION: main
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): java.lang.NullPointerException
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.widget.ImageSwitcher.setImageDrawable(ImageSwitcher.java:55)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at com.paravegetarianos.motores.DescargadorImagenes.onPostExecute(DescargadorImagenes.java:66)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at com.paravegetarianos.motores.DescargadorImagenes.onPostExecute(DescargadorImagenes.java:1)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.os.AsyncTask.finish(AsyncTask.java:417)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.os.Looper.loop(Looper.java:123)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at java.lang.reflect.Method.invoke(Method.java:521)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201):     at dalvik.system.NativeStart.main(Native Method)

你能找到这段代码的任何问题吗(我敢打赌这将是一个非常愚蠢和明显的错误)?谢谢大家.

Can you find any (I bet it will be a really stupid and obvious mistake) problem with this code? Thanks to all.

PS:错误行(66 和 90)是调用 setImageDrawable 和 setImageResource 的地方.如果您认为需要更多代码,请告诉我,我会附加.

PS: The error lines (66 and 90) are where setImageDrawable and setImageResource are called. If you think that more code is needed, please tell me and I'll append it.

推荐答案

您需要通过设置 ViewFactory 或调用 ImageSwitcher 添加视图addView 两次.ViewSwitcher 文档中的更多信息.

You need to add views to your ImageSwitcher, either by setting a ViewFactory or calling addView twice. More info in the docs for ViewSwitcher.

例如,查看您问题中的最后一个堆栈跟踪,NPE 发生在 ImageSwitcher.java 的第 55 行,即:

For example, looking at the last stack trace in your question, the NPE happens at line 55 of ImageSwitcher.java, which is:

public void setImageDrawable(Drawable drawable)
{
    ImageView image = (ImageView)this.getNextView();
    image.setImageDrawable(drawable);  // <--- line 55
    showNext();
}

image 为空,因为尚未设置视图.

image is null because the views have not been set.

这篇关于在 ImageSwitcher 中 setImageDrawable 和 setImageResource 时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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