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);
}
}
Here's the 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)
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);
}
}
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);
}
});
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: 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.
You need to add views to your
ImageSwitcher
, either by setting aViewFactory
or callingaddView
twice. More info in the docs for ViewSwitcher.For example, looking at the last stack trace in your question, the NPE happens at line 55 of ImageSwitcher.java, which is:
image
is null because the views have not been set.