如何在ViewHolder上修复“ NullPointerException”?

时间:2019-05-16 23:18:56

标签: java android android-recyclerview nullpointerexception

我正在尝试执行可点击的RecyclerView,我观看了tutorial的操作方法,但是当我点击NullPointerException时,它为我提供了RecyclerView界面

public class ViewHolder extends RecyclerView.ViewHolder {

    View mView;
    private ViewHolder.ClickListener mClickListener;

    public interface ClickListener{
        void onItemClick(View view, int position);
    }

    public void setOnClickListener(ViewHolder.ClickListener clickListener){
        mClickListener = clickListener;
    }

    public ViewHolder(View itemView){
        super(itemView);

        mView = itemView;

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(v, getAdapterPosition()); //Line that called the Exception
            }
        });
    }
}

与界面有关吗?我是否需要像this one建议那样将接口传递给适配器?

这是个例外

java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.jobseeker.ViewHolder$ClickListener.onItemClick(android.view.View, int)' on a null object reference
        at com.example.jobseeker.ViewHolder$1.onClick(ViewHolder.java:32)
...etc.

这是适配器

adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup viewGroup, int i) {
        final View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.row, viewGroup, false);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView mCompNameTv = view.findViewById(R.id.rCompNameTv);
                TextView mDescTv = view.findViewById(R.id.rDescTv);
                TextView mPosTv = view.findViewById(R.id.rPosTv);
                ImageView mImageTv = view.findViewById(R.id.rImageIv);

                String mCompName = mCompNameTv.getText().toString();
                String mDesc = mDescTv.getText().toString();
                String mPos = mPosTv.getText().toString();
                Drawable mDrawable = mImageTv.getDrawable();
                Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();

                Intent intent = new Intent(viewGroup.getContext(), DetailActivity.class);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                mBitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
                byte[] bytes = stream.toByteArray();
                intent.putExtra("image", bytes);
                intent.putExtra("companyName", mCompName);
                intent.putExtra("description", mDesc);
                intent.putExtra("position", mPos);
                startActivity(intent);
            }
        });

        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) {
        holder.setDetails(model.getCompanyName(), model.getDescription(), model.getPosition(), model.getImage());
    }

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

1 个答案:

答案 0 :(得分:0)

很可能您尚未使用listener属性初始化ViewHolder类。使用setOnClickListener函数初始化ViewHolder

因此,您来自onCreateViewHolder的return语句可能如下所示。

ViewHolder vh = new ViewHolder(view);
vh.setOnClickListener(clickListener); // Initialize a click listener in your Activity maybe and pass it here.
return vh;

我建议您检查my answer here,以更好地了解如何在RecyclerView中实现点击监听。