尝试用资源填充ImageSwitcher时,我不断收到NullPointerException错误。在WeakReference期间,使用AsyncTask中的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。

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


结果是在onPostExecute()步骤中捕获了位图。我调用AsyncTask的时刻是在构建Gallery的onItemSelectedListener()时:

    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。

这是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的调用位置。如果您认为需要更多代码,请告诉我,我将其附加。

最佳答案

您需要通过设置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为null,因为尚未设置视图。

关于android - ImageSwitcher中的setImageDrawable和setImageResource时发生NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5067677/

10-13 04:38