null pointer exception while trying to add dynamic views to view flipper using universal image loader library android

亡梦爱人 提交于 2019-12-12 06:48:34

问题


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



来源:https://stackoverflow.com/questions/15430426/null-pointer-exception-while-trying-to-add-dynamic-views-to-view-flipper-using-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!