null pointer exception while trying to add dynamic

2019-09-20 09:58发布

问题:

I am trying to create a viewflipper with dynamic imageviews the src of the imageviews is coming from the sd card path.I want to load the images using Universal image loader library. I am getting null pointer exception when I am binding the image to the imageviews.

below is my code

 private class someTask extends AsyncTask<Void, Void, Void> {

@Override
    protected Void doInBackground(Void... params) {

        getPages();
            return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        setPageLocations();
        dismissProgress();
    }
}


 private void getPages()
{


     while(mCursor.moveToNext()){
         String pageLocation = get images from database;
                gallery_grid_Images[i] = pageLocation;

         i++;
     }
}

private void setPageLocations(){

    for (int i=0;i<gallery_grid_Images.length;i++){
        /* Bitmap bitmap = BitmapFactory.decodeFile(gallery_grid_Images[i]);
          BitmapDrawable bd = new BitmapDrawable(bitmap);
            ImageView imageView = new ImageView(getApplicationContext());
           imageView.setImageDrawable(bd);*/
   imageLoader.displayImage(gallery_grid_Images[i], imageView,options);

          viewFlipper.addView(imageView);
    }
}

Any help would be highly appreciated. A link to a viewflipper example using universal image loader will be helpful as well.

Thanks.

PS: it works fine for the commented code.but since there are lot of images I don't want to run into outofmemory error.

below is the stacktrace

03-17 02:37:16.852: E/AndroidRuntime(476): FATAL EXCEPTION: main
03-17 02:37:16.852: E/AndroidRuntime(476): java.lang.NullPointerException
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.nostra13.universalimageloader.core.ImageLoader.getImageSizeScaleTo(ImageLoader.java:453)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:214)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:153)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.sofi.magz.networkusage.MagzineReader.setPageLocations(MagzineReader.java:177)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.sofi.magz.networkusage.MagzineReader.access$1(MagzineReader.java:167)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.sofi.magz.networkusage.MagzineReader$SetMagzineReaderTask.onPostExecute(MagzineReader.java:119)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.sofi.magz.networkusage.MagzineReader$SetMagzineReaderTask.onPostExecute(MagzineReader.java:1)
03-17 02:37:16.852: E/AndroidRuntime(476):  at android.os.AsyncTask.finish(AsyncTask.java:417)
03-17 02:37:16.852: E/AndroidRuntime(476):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-17 02:37:16.852: E/AndroidRuntime(476):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-17 02:37:16.852: E/AndroidRuntime(476):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 02:37:16.852: E/AndroidRuntime(476):  at android.os.Looper.loop(Looper.java:130)
03-17 02:37:16.852: E/AndroidRuntime(476):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-17 02:37:16.852: E/AndroidRuntime(476):  at java.lang.reflect.Method.invokeNative(Native Method)
03-17 02:37:16.852: E/AndroidRuntime(476):  at java.lang.reflect.Method.invoke(Method.java:507)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-17 02:37:16.852: E/AndroidRuntime(476):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-17 02:37:16.852: E/AndroidRuntime(476):  at dalvik.system.NativeStart.main(Native Method)

setpagelocation is called at 167.

回答1:

I solved the above error by adding the view before calling the displayImage of universal image loader.



回答2:

Set it LayoutParams,

...

imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

imageLoader.displayImage(gallery_grid_Images[i], imageView,options); viewFlipper.addView(imageView);

...



回答3:

adding the view before calling the displayImage of universal image loader