Reputation: 251
I have setup a GridView
to hold some images that are loaded from an SD Card. However, there are a lot of images to be loaded and therefore I decided to run the loading and resampling tasks on a background thread using AsyncTask
and adding the image into the GridView
once image is done being loaded. Unfortunately whenever I run the code I run into this error:
01-01 19:49:40.239: E/AndroidRuntime(3851): FATAL EXCEPTION: main
01-01 19:49:40.239: E/AndroidRuntime(3851): java.lang.NullPointerException
01-01 19:49:40.239: E/AndroidRuntime(3851): at com.stullich.tim.woistmeinphoto.PhotoGalleryActivity.addImage(PhotoGalleryActivity.java:199)
01-01 19:49:40.239: E/AndroidRuntime(3851): at <packagename>.PhotoGalleryActivity.access$0(PhotoGalleryActivity.java:195)
01-01 19:49:40.239: E/AndroidRuntime(3851): at <packagename>.PhotoGalleryActivity$FileFetcher.onProgressUpdate(PhotoGalleryActivity.java:226)
01-01 19:49:40.239: E/AndroidRuntime(3851): at <packagename>.PhotoGalleryActivity$FileFetcher.onProgressUpdate(PhotoGalleryActivity.java:1)
01-01 19:49:40.239: E/AndroidRuntime(3851): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:618)
01-01 19:49:40.239: E/AndroidRuntime(3851): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 19:49:40.239: E/AndroidRuntime(3851): at android.os.Looper.loop(Looper.java:137)
01-01 19:49:40.239: E/AndroidRuntime(3851): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-01 19:49:40.239: E/AndroidRuntime(3851): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 19:49:40.239: E/AndroidRuntime(3851): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 19:49:40.239: E/AndroidRuntime(3851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-01 19:49:40.239: E/AndroidRuntime(3851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-01 19:49:40.239: E/AndroidRuntime(3851): at dalvik.system.NativeStart.main(Native Method)
This is the code that seems to be causing the trouble:
final class FileFetcher extends AsyncTask<Void, LoadedImage, Void>
{
private Context mContext;
public FileFetcher(Context context)
{
mContext = context;
}
@Override
protected void onPreExecute()
{
loadImages(new File("/mnt/extSdCard/Bilder"));
}
@Override
public void onProgressUpdate(LoadedImage... value)
{
addImage(value);
}
@Override
protected Void doInBackground(Void... rootDir)
{
int i = 1;
for (File f : directories)
{
File[] anImage = f.listFiles();
if (anImage.length > 0)
{
Bitmap aMap = decodeSampledBitmapFromFile(anImage[0], 120, 120);
publishProgress(new LoadedImage(aMap));
i++;
}
}
return null;
}
}
//My addImage method
private void addImage(LoadedImage... value)
{
for (LoadedImage image : value)
{
adapter.addPhoto(image);
adapter.notifyDataSetChanged();
}
}
The crash seems to happen every time the addImage
method is called. A Bitmap is always returned so I have no idea where the NullPointerException
can be thrown. Any help would be much appreciated since I have been sitting at this problem for quite a while.
Upvotes: 0
Views: 230
Reputation: 251
I forgot to initialize my adapter in onCreate
which caused the application to crash.
Upvotes: 4