0

I have a problem, i read about it and i cant do that on OnBindViewHolder, but i use this in other sites and always same error... After my error i use too, on LONG click to do one thing and it work correctly but before that i want to use on Clock normal, not long, for when i click, show an alertbox with image (i try to do this with imageview normal too, without alertbox) but nothing, same error...

I want with this to take on one photo of recyclerview, and open it from directory and "zoom" in alertbox...

The code

public class Fotos extends RecyclerView.Adapter<Fotos.ViewHolder> {

    private Context context;
    private List<ImageItem> fotosLista;
    private ImageButton image;


    public Fotos(List<ImageItem> fotosLista, Context context) {
        this.fotosLista = fotosLista;
        this.context = context;


    }


    public Fotos() {

    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public View view;
       public ImageButton image;




        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            image = itemView.findViewById(R.id.imagen);


        }

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View itemView = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.grid_item, viewGroup, false);

        ViewHolder tvh = new ViewHolder(itemView);


        return tvh;

    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int pos) {

        final ImageItem item = fotosLista.get(pos);
        image = viewHolder.view.findViewById(R.id.imagen);
        image.setImageBitmap(item.getImage());
        image.setImageBitmap(Bitmap.createScaledBitmap(item.getImage(), 120, 120, false));


        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                File[] listFile;
                File file = new File("storage/emulated/0/Pictures/myDirectoryName/");
                listFile = file.listFiles();
                for (int i = 0; i < listFile.length; i++) {
                    String path = listFile[i].getAbsolutePath();
                    Bitmap bitmapMostrar = BitmapFactory.decodeFile(path);


                    ImageView mostrarImagen=(ImageView)viewHolder.view.findViewById(R.id.mostrarImagen2);
                    mostrarImagen.setImageBitmap(bitmapMostrar);


                }
                    AlertDialog.Builder transferencia2 = new AlertDialog.Builder(viewHolder.view.getContext());
                    transferencia2.setView(R.layout.imagenzoom);
                    AlertDialog dialog2 = transferencia2.create();
                    dialog2.show();




            }
        });


        viewHolder.image.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {


                AlertDialog.Builder transferencia = new AlertDialog.Builder(viewHolder.view.getContext());
                transferencia.setMessage("¿Deseas enviar la foto al servidor?");
                transferencia.setTitle("Transferir/Cancelar ");
                transferencia.setPositiveButton("Transferir", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        dialogInterface.cancel();


                        //Aquí va lo de la BD + Update




                    }


                });
                transferencia.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        //Aquí se cierra el alertBox y se cancela la transferencia.

                        dialogInterface.cancel();


                    }
                });

                AlertDialog dialog2 = transferencia.create();
                dialog2.show();


                return true;
            }
        });


    }


    @Override
    public int getItemCount() {
        return (null != fotosLista ? fotosLista.size() : 0);
    }

The problem is on OnBindViewHolder, on first onClick method... *LogCat error:*

05-08 17:26:47.870 14576-14576/com.example.practicas_.arcadiatruck E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.practicas_.arcadiatruck, PID: 14576
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
    at com.example.practicas_.arcadiatruck.Fotos$1.onClick(Fotos.java:105)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22433)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-08 17:26:47.873 1135-4779/? W/ActivityManager:Force finishing activity com.example.practicas_.arcadiatruck/.ActividadFragments

The alertbox is a layout with only one image, i test it with android:src and drawable image on it and i click and it work... Thank u guys, i lose a lot of hours with this xD

Chuflitas
  • 105
  • 1
  • 11
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM May 08 '18 at 15:37

1 Answers1

0

Remove this line

image = viewHolder.view.findViewById(R.id.imagen);

And when you use setImageBitmap access the image like that

viewHolder.image.setImageBitmap(yourImage);

EDIT:

There is a lot of stuff happening in your onBindViewHolder and you inflate your views (findViewById) in different places. All that work should be moved to a ViewHolder it will be much easier to manage.

First of all, you have this line twice image = viewHolder.view.findViewById(R.id.imagen);, you don't need to do that, you only need to do it once, do it once in ViewHolder constructor and that's it.

Also this line ImageView mostrarImagen=(ImageView)viewHolder.view.findViewById(R.id.mostrarImagen2); is better off in your ViewHolder constructor and not in onBindViewHolder.

After that, remove the View view from your ViewHolder and remove this line from the constructor as well view = itemView;, no need to do that.

Now your ViewHolder constructor and declaration should look something like this:

public ImageButton image;
public ImageView mostrarImagen

public ViewHolder(View itemView) {
    super(itemView);
    image = itemView.findViewById(R.id.imagen);
    mostrarImagen = itemView.findViewById(R.id.mostrarImagen2);
}

And when you use the ImageButton and the ImageView in onBindViewHolder don't do findViewById just use viewHolder.image and viewHolder.mostrarImagen.

Regarding your onClickListener and onLongClickListener, put them in your ViewHolder class, it's much cleaner. Put these lines in your ViewHolder constructor:

itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);

Also, make sure that your ViewHolder implements onClickListener like this:

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener

And then in your ViewHolder class override onClickListener and onLongClickListener:

@Override
public void onClick(View view) {
    // your code        
}

@Override
public boolean onLongClick(View view) {
    // your code
    return true;
}
Suleyman
  • 2,765
  • 2
  • 18
  • 31