0

There is a custom listview which displays image and text. I am using Glide image library to load the image. So when i search the image and text displays in listview but before image loads if i click on it Java Null Pointer exception is seen. How to avoid making click before image loads or any change is required to done on the itemclick listener.

ItemClick:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    descusers du = dusers.get(position);

    final String Limage = du.image;


    if (!Limage.isEmpty()) {
    final ImageView imageView1 = (ImageView) view.findViewById(R.id.imageList);
    final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) imageView1.getDrawable();
    final Bitmap yourBitmap = bitmapDrawable.getBitmap();

    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(yourBitmap);
    p=new PhotoViewAttacher(imageView);


    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();

    }
}

I am checking if there is image url then fetch the image after loading using Glide, but exception when i click before image loads. any suggestion?

Answer:

Created Onclicklistener in Adapter passing display activity.

viewholder.iview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog builder = new Dialog(listdisplay);
                builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                builder.getWindow().setBackgroundDrawable(
                        new ColorDrawable(android.graphics.Color.TRANSPARENT));
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        //nothing;
                    }
                });
                ImageView imageView = new ImageView(listdisplay);

                final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) finalViewholder.iview.getDrawable();
                final Bitmap yourBitmap = bitmapDrawable.getBitmap();
                imageView.setImageBitmap(yourBitmap);
                p=new PhotoViewAttacher(imageView);


                builder.addContentView(imageView, new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                builder.show();
            }
        });

Adapter constructor:

private Activity listdisplay;
public DescAdapter(listdisplay ds, ArrayList<descusers> dusers,Activity listdisplay) {
    this.ds = ds;
    this.dusers = dusers;
    this.listdisplay = listdisplay;

}
Sam Judd
  • 7,317
  • 1
  • 38
  • 38
user2269164
  • 1,095
  • 2
  • 15
  • 31
  • java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable.getBitmap()' on a null object reference – user2269164 May 12 '16 at 08:51
  • as error suggested you have null reference to your imageView1 @user2269164 – KDeogharkar May 12 '16 at 08:58
  • Edit your message and add properly your logcat, nobody will read your messages, and second, show us your layout. We need 0s and 1s not guesses. – Andrei T May 12 '16 at 09:04
  • you are getting NPE because you are trying to read image drawable which is really not downloaded or downloading. – Bharatesh May 12 '16 at 12:18

2 Answers2

0

Try with this code on your getView()

@Override public View getView(int position, View convertView, ViewGroup parent) {
final T item = getItem(position);
....
Glide.with(getContext())
    .load(UIUtils.getThumbnailImageUrl(item.images))
    .placeholder(R.drawable.default_thumbnail_picture)
    .crossFade()
    .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    })
    .into(viewHolder.mExerciseImageView);
return convertView;
}

This may helps you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

you can check if the image is null or not. may be there will be callbacks in the library you are using(i'm not sure)

final Bitmap yourBitmap = bitmapDrawable.getBitmap();
if(yourBitmap==null){
 image.setEnabled(false);
}

so you can avoid the click if the image is not loaded. or you can setEnabled(false) by default and when the image loaded use setEnabled(true)

Devendra Singh
  • 2,343
  • 4
  • 26
  • 47