我有一个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();
}
}
请告诉我为什么会收到此错误。
抱歉问题格式不好,我还在学习如何发布好问题。
提前致谢。
答案 0 :(得分:0)
mapFragment
为空。 findFragmentById
如果找不到该片段,则可以返回null(例如,因为它未附加到活动中)