单击Fragment上RecyclerView的适配器(照片)的ITEM时显示ImageView

时间:2018-05-08 15:33:24

标签: android android-recyclerview nullpointerexception adapter android-viewholder

我有一个问题,我读到它并且我无法在OnBindViewHolder上执行此操作,但我在其他网站中使用此功能并始终出现相同的错误... 在我使用错误之后,在LONG点击做一件事并且它正常工作但在此之前我想在时钟上使用正常,不长,当我点击时,显示带图像的警告框(我尝试用imageview执行此操作)正常,没有警告框) 但没有,同样的错误...

我希望用这张照片拍摄一张recyclerview照片,然后从目录中打开并在alertbox中“缩放”......

代码

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);
    }

问题出现在OnBindViewHolder上,首先是onClick方法... * LogCat错误:*

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

警报框是一个只有一个图像的布局,我用android:src测试它并在其上绘制可绘图像,然后点击它就可以了... 谢谢你们,我用这个xD失去了很多时间

1 个答案:

答案 0 :(得分:0)

删除此行

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

当您使用setImageBitmap访问image

viewHolder.image.setImageBitmap(yourImage);

修改

您的onBindViewHolder中发生了很多事情,并且您在不同的地方夸大了您的观点(findViewById)。所有这些工作都应该移到ViewHolder,这将更容易管理。

首先,你有两行image = viewHolder.view.findViewById(R.id.imagen);,你不需要这样做,你只需要做一次,在ViewHolder构造函数中执行一次,就是这样。< / p>

此行ImageView mostrarImagen=(ImageView)viewHolder.view.findViewById(R.id.mostrarImagen2);ViewHolder构造函数中更好,而不是onBindViewHolder

之后,从您的View view中删除ViewHolder,并从构造函数view = itemView;中删除此行,无需执行此操作。

现在,您的ViewHolder构造函数和声明应如下所示:

public ImageButton image;
public ImageView mostrarImagen

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

当您使用ImageButtonImageView中的onBindViewHolder时,findViewById只使用viewHolder.imageviewHolder.mostrarImagen。< / p>

关于您的onClickListeneronLongClickListener,请将它们放在ViewHolder课程中,它更清晰。将这些行放在ViewHolder构造函数中:

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

另外,请确保您的ViewHolder实施onClickListener,如下所示:

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

然后在ViewHolder班级覆盖onClickListeneronLongClickListener

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

@Override
public boolean onLongClick(View view) {
    // your code
    return true;
}