我正在尝试使用适配器将RecyclerView中的项目填满;但是,它一直以为我有一个空引用。
运行错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
这是我用来填充适配器的片段的代码
MembersFragmentGaeilge
public class MembersFragmentGaeilge extends Fragment {
View v;
RecyclerView rvCarlowKilkenny;
public MembersFragmentGaeilge() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_members_gaeilge, container, false);
rvCarlowKilkenny = v.findViewById(R.id.rv_Carlow_Kilkenny_Gaeilge);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
rvCarlowKilkenny.setLayoutManager(gridLayoutManager);
List<MemberData> mCarlowKilkenny;
MemberData mMemberList;
mCarlowKilkenny = new ArrayList<>();
mMemberList = new MemberData("Micheal","Independent","Kerry", "SandymCo. Kerry", "michael.healy-raoireachtas.ie",
"dfdfd","defewfwe",R.drawable.constituency_laois_offaly);
mCarlowKilkenny.add(mMemberList);
MembersAdapter carlowKilkennyAdapter = new MembersAdapter(getContext(),mCarlowKilkenny);
rvCarlowKilkenny.setAdapter(carlowKilkennyAdapter);
return v;
}
}
这是我使用的适配器的代码
该项layout_members只是我希望在其中显示代码的地方,因此导致其在MemberFragmentGaeilge的整个页面上都处于网格布局中
MembersAdapter.java
public class MembersAdapter extends RecyclerView.Adapter<MemberHolder> {
private Context mContext;
private List<MemberData> mMemberList;
public MembersAdapter(Context mContext, List<MemberData> mMemberList){
this.mContext = mContext;
this.mMemberList = mMemberList;
}
@Override
public MemberHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_members,viewGroup,false);
return new MemberHolder(mView);
}
@Override
public void onBindViewHolder(@NonNull MemberHolder memberHolder, int i) {
memberHolder.mName.setText(mMemberList.get(i).getMemberName());
memberHolder.mParty.setText(mMemberList.get(i).getMemberParty());
memberHolder.mConstituency.setText(mMemberList.get(i).getMemberConstituency());
memberHolder.mAddress.setText(mMemberList.get(i).getMemberAddress());
memberHolder.mEmail.setText(mMemberList.get(i).getMemberEmail());
memberHolder.mPhone.setText(mMemberList.get(i).getMemberPhone());
memberHolder.mContributions.setText(mMemberList.get(i).getMemberContributions());
memberHolder.imgMember.setImageResource(mMemberList.get(i).getMemberImage());
memberHolder.mCardView.setOnClickListener(new View.OnClickListener() {
//Passing the values over to the next class
@Override
public void onClick(View v) {
/* Intent intent = new Intent(mContext,MembersFragmentGaeilge.class);
intent.putExtra("memberName",mMemberList.get(memberHolder.getAdapterPosition()).getMemberName());
intent.putExtra("memberParty",mMemberList.get(memberHolder.getAdapterPosition()).getMemberParty());
intent.putExtra("memberConstituency",mMemberList.get(memberHolder.getAdapterPosition()).getMemberConstituency());
intent.putExtra("memberAddress",mMemberList.get(memberHolder.getAdapterPosition()).getMemberAddress());
intent.putExtra("memberEmail",mMemberList.get(memberHolder.getAdapterPosition()).getMemberEmail());
intent.putExtra("memberPhone",mMemberList.get(memberHolder.getAdapterPosition()).getMemberPhone());
intent.putExtra("memberContributions",mMemberList.get(memberHolder.getAdapterPosition()).getMemberContributions());
mContext.startActivity(intent);*/
}
});
}
@Override
public int getItemCount() {
return mMemberList.size();
}
}
class MemberHolder extends RecyclerView.ViewHolder {
ImageView imgMember;
TextView mName, mParty, mConstituency, mAddress, mEmail, mPhone, mContributions;
CardView mCardView;
public MemberHolder (View itemView){
super(itemView);
mName = itemView.findViewById(R.id.tvMemberName);
mParty = itemView.findViewById(R.id.tvMemberParty);
mConstituency = itemView.findViewById(R.id.tvMemberConstituency);
mAddress = itemView.findViewById(R.id.tvMemberPhone);
mEmail = itemView.findViewById(R.id.tvMemberEmail);
mPhone = itemView.findViewById(R.id.tvMemberPhone);
mContributions = itemView.findViewById(R.id.tvMemberContributions);
imgMember = itemView.findViewById(R.id.imgMemberOireachtas);
mCardView = itemView.findViewById(R.id.cvMemberOireachtas);
}
}
检查您的mAdress id and mPhone id these two have same id thats the reason it show the error