此问题已在此处找到答案:
什么是nullpointerexception,如何修复它((12个答案)
6天前关门。
我已经使用android studio在java中构建了一个RecycleWeber应用程序。当我运行应用程序时,它崩溃了,当我检查logcat时,我发现我的适配器中有一个空指针异常。 onBindViewHolder
方法。这包括第49行。
@Override
public void onBindViewHolder(@NonNull ContactsAdapter.ViewHolder holder, int position) {
holder.txtName.setText(contacts.get(position).getName());
holder.txtName.setText(contacts.get(position).getEmail());
Glide.with(context).asBitmap().load(contacts.get(position).getImage()).into(holder.image);
holder.parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, contacts.get(position).getName() + " Clicked", Toast.LENGTH_SHORT).show();
}
});
}
logcat日志
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.learningapplication.ContactsAdapter.onBindViewHolder(ContactsAdapter.java:49)
at com.example.learningapplication.ContactsAdapter.onBindViewHolder(ContactsAdapter.java:19)
编辑:以下是完整代码:
package com.example.learningapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder>{
private Context context;
ArrayList<Contacts> contacts = new ArrayList<>();
public void setContacts(ArrayList<Contacts> contacts) {
this.contacts = contacts;
notifyDataSetChanged();
}
public ContactsAdapter(Context context) {
this.context = context;
}
public ContactsAdapter() {
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ContactsAdapter.ViewHolder holder, int position) {
holder.txtName.setText(contacts.get(position).getName());
holder.txtName.setText(contacts.get(position).getEmail());
Glide.with(context).asBitmap().load(contacts.get(position).getImage()).into(holder.image);
holder.parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, contacts.get(position).getName() + " Clicked", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return contacts.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtName;
private TextView txtEmail;
private ImageView image;
private CardView parent;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtName);
txtEmail = itemView.findViewById(R.id.txtEmail);
image = itemView.findViewById(R.id.image);
}
}
}
``
1条答案
按热度按时间7hiiyaii1#
据我了解:
holder.parent
-这是null
检查您的初始化