在ImageSwitcher中设置setImageDrawable和setImageResource时出现NullPointerException

时间:2011-02-21 15:18:24

标签: android nullpointerexception

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

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

这是pre stacktrace:

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。

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

结果是onPostExecute()步骤中捕获的位图。我调用AsyncTask的那一刻是onItemSelectedListener(),当构建一个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是图库,seleccionarImagen(int)获取图像索引并调用AsyncTask。

这是post stacktrace(在评论错误行之后):

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)

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

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

1 个答案:

答案 0 :(得分:3)

您需要为ImageSwitcher添加观看次数,方法是设置ViewFactory或致电addView两次。 docs for ViewSwitcher

中的更多信息

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

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

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