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