繁体   English   中英

在RecyclerView适配器中收到“尝试调用虚拟方法”错误。 请查看详细信息

[英]Getting a 'Attempt to invoke virtual method' error in RecyclerView Adapter. Please see details

我有一个带有适配器的recyclerview。

问题是我遇到此错误: Attempt to invoke virtual method 'android.support.v4.app.FragmentActivity android.support.v4.app.Fragment.getActivity()' on a null object reference at com.abc.xyz.RVAdapterAAR.onBindViewHolder(RVAdapterAAR.java:82)中的以下代码:

public class RVAdapterAAR extends RecyclerView.Adapter<RVAdapterAAR.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        TextView nPicTag;
        ImageView hImage;
        TextView nDescriptionTag;
        TextView hDescription;
        TextView hLocation;
        SupportMapFragment mapFragment;
        Button btn_accept;
        Button btn_share;
        TextView postDate;
        TextView postTime;
        TextView postedBy;

        PersonViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.card_accept_request);
            nPicTag = (TextView) itemView.findViewById(R.id.needy_pic_tag);
            hImage = (ImageView) itemView.findViewById(R.id.h_pic_accept);
            nDescriptionTag = (TextView) itemView.findViewById(R.id.n_description_tag);
            hDescription = (TextView) itemView.findViewById(R.id.h_description_accept);
            hLocation = (TextView) itemView.findViewById(R.id.currentCoordinatesTagAccept);
            mapFragment = (SupportMapFragment) mapFragment.getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment);
            btn_accept = (Button) itemView.findViewById(R.id.btn_accept);
            btn_share = (Button) itemView.findViewById(R.id.btn_share);
            postDate = (TextView) itemView.findViewById(R.id.post_date);
            postTime = (TextView) itemView.findViewById(R.id.post_time);
            postedBy = (TextView) itemView.findViewById(R.id.posted_by);
        }

    }

    List<ListContentAAR> listContentAARs;

    RVAdapterAAR(List<ListContentAAR> listContentAARs) {
        this.listContentAARs = listContentAARs;
    }

    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_accept_a_request, viewGroup, false);
        PersonViewHolder personViewHolder = new PersonViewHolder(view);
        return personViewHolder;
    }

    public void onBindViewHolder (PersonViewHolder personViewHolder, int i) {
        personViewHolder.nPicTag.setText(listContentAARs.get(i).nPicTag);
        personViewHolder.hImage.setImageBitmap(listContentAARs.get(i).hImage);
        personViewHolder.nDescriptionTag.setText(listContentAARs.get(i).nDescriptionTag);
        personViewHolder.hDescription.setText(listContentAARs.get(i).hDescription);
        personViewHolder.hLocation.setText(listContentAARs.get(i).hLocation);
        personViewHolder.mapFragment.getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment);
        personViewHolder.btn_accept.findViewById(R.id.btn_accept);
        personViewHolder.btn_share.findViewById(R.id.btn_share);
        personViewHolder.postDate.setText(listContentAARs.get(i).postDate);
        personViewHolder.postTime.setText(listContentAARs.get(i).postTime);
        personViewHolder.postedBy.setText(listContentAARs.get(i).postedBy);
    }

    public int getItemCount() {
        return listContentAARs.size();
    }

}

请让我知道为什么我会收到此错误。

抱歉,问题格式错误,我仍在学习如何发布好的问题。

提前致谢。

mapFragment为null。 如果找不到片段,则findFragmentById可以返回null(例如,因为它未附加到活动中)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM