从RecyclerView

时间:2016-08-08 15:38:15

标签: android imageview android-recyclerview

我的目标是分享ImageViewRecyclerView内的图片 使用Picasso lib

从Internet加载图像

我在每个项目下方添加了一个“分享”按钮 正确设置了onclick功能 但是不能再进一步了。

我有这个数据适配器:

 package com.myapp.app;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.app.model.AndroidVersion;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private ArrayList<AndroidVersion> android;
    private Context context;
    private ImageView content;



    public DataAdapter(Context context,ArrayList<AndroidVersion> android) {
        this.android = android;
        this.context = context;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {


        viewHolder.tv_name.setText(android.get(i).getName());
        viewHolder.tv_version.setText(android.get(i).getVer());
        Picasso.with(context).load(android.get(i).getApi()).into(viewHolder.img_android);
        Picasso.with(context).load(android.get(i).getImage()).into(viewHolder.image_android);


        viewHolder.btshare.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View v) {
            /// button click event
                content = (ImageView) v.findViewById(R.id.image_android);
                content.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
                File cachePath = new File("/DCIM/Camera/image.jpg");
                try {
                    cachePath.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(cachePath);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                    ostream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }



                Intent share = new Intent(Intent.ACTION_SEND);
                Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
                share.setData(phototUri);
                share.setType("image/*");
                share.putExtra(Intent.EXTRA_STREAM, phototUri);
                v.getContext().startActivity(Intent.createChooser(share,"Share via"));


        }
        });



    }

    @Override
    public int getItemCount() {
        return android.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_name,tv_version;
        private ImageView img_android,image_android;
        private Button btshare;

        public ViewHolder(View view) {
            super(view);
            btshare = (Button) view.findViewById(R.id.btshare);
            tv_name = (TextView)view.findViewById(R.id.tv_name);
            tv_version = (TextView)view.findViewById(R.id.tv_version);
            img_android = (ImageView)view.findViewById(R.id.img_android);
            image_android = (ImageView)view.findViewById(R.id.image_android);


        }
    }


}

只要我点击“

”,它就会关闭应用程序

请帮助

这是错误日志行:

08-08 21:10:59.956 8934-8934/com.myapp.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.app, PID: 8934
java.lang.NullPointerException: Attempt to invoke virtual method 
'void android.widget.ImageView.setDrawingCacheEnabled(boolean)' on a null object reference
at com.myapp.app.DataAdapter$1.onClick(DataAdapter.java:60)
at android.view.View.performClick(View.java:5232)
at android.view.View$PerformClick.run(View.java:21289)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

3 个答案:

答案 0 :(得分:1)

content = (ImageView) v.findViewById(R.id.image_android)更改为

content = (ImageView) viewholder.image_android;

您的imageView在itemView中,代码正在尝试在按钮内找到它。

答案 1 :(得分:1)

问题很容易解决。

只需更改

content = (ImageView) v.findViewById(R.id.image_android);

content = viewHolder.image_android;

因为您在这里引用了错误的View v.findViewById(R.id.image_android)

此处v引用Button视图,您试图在Button内找到。 但是您的实际图片不在Button 中,是吗?

只需从ViewHolder中引用image_android,即可正确引用它。 它不再是空的

答案 2 :(得分:-1)

OncLickListener中设置ViewHolder,而不是onBindViewHolder