0

I have a recyclerview for which I have an adapter.

The problem is that I am getting this error: 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) in the following code:

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

}

Please let me know why am I getting this error.

Sorry for bad format of question, I'm still learning how to post good questions.

Thanks in advance.

  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Yaw Asare Jan 17 '16 at 04:07

1 Answers1

0

mapFragment is null. findFragmentById can return null if it doesn't find the fragment (because it's not attached to the activity, for instance)

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • the `mapFragment` is in xml file of one of the two fragments attached with main activity. There are 2 tabs actually. –  Jan 17 '16 at 04:05