我正在将Android RecyclerView与多个ViewHolders一起使用。 当我设置适配器时,出现以下错误:
java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView' on a null object reference
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6686)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
at android.view.View.measure(View.java:22071)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1308)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:350)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
at android.view.View.measure(View.java:22071)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
03-04 19:08:44.817 3862-3862/com.bocawestcc.cobaltmemberapp E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
这是适配器类:
public class EventModifyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int VIEW_TYPE_MEMBER = 0;
final int VIEW_TYPE_GUEST = 1;
final int VIEW_TYPE_BUDDY = 2;
private final static int TYPE_CALL=1,TYPE_SMS=2;
private List<Object> modifyListData =new ArrayList();
// Context is not used here but may be required to
// perform complex operations or call methods from outside
private Context context;
// Constructor
public EventModifyAdapter(Context context){
this.context=context;
}
public void setEventModifyData(List<Object> modifyListData){
this.modifyListData = modifyListData;
}
// We need to override this as we need to differentiate
// which type viewHolder to be attached
// This is being called from onBindViewHolder() method
@Override
public int getItemViewType(int position) {
if (modifyListData.get(position) instanceof MemberList) {
return VIEW_TYPE_MEMBER;
} else if (modifyListData.get(position) instanceof GuestList) {
return VIEW_TYPE_GUEST;
} else if (modifyListData.get(position) instanceof BuddyList) {
return VIEW_TYPE_BUDDY;
}
return -1;
}
// Invoked by layout manager to replace the contents of the views
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
int viewType=holder.getItemViewType();
switch (viewType){
case VIEW_TYPE_MEMBER:
MemberList memberListList=(MemberList) modifyListData.get(position);
((MemberViewHolder)holder).showMemberDetails(memberListList);
break;
case VIEW_TYPE_GUEST:
GuestList guestListList=(GuestList) modifyListData.get(position);
((GuestViewHolder)holder).showGuestDetails(guestListList);
break;
case VIEW_TYPE_BUDDY:
BuddyList buddyList=(BuddyList) modifyListData.get(position);
((BuddyViewHolder)holder).showBuddyDetails(buddyList);
break;
}
}
@Override
public int getItemCount(){return modifyListData.size();}
// Invoked by layout manager to create new views
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Attach layout for single cell
int layout = 0;
RecyclerView.ViewHolder viewHolder;
// Identify viewType returned by getItemViewType(...)
// and return ViewHolder Accordingly
switch (viewType){
case VIEW_TYPE_MEMBER:
layout = R.layout.row_all_members;
View membersView = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new MemberViewHolder(membersView);
break;
case VIEW_TYPE_GUEST:
layout = R.layout.row_all_guests;
View guestsView = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new GuestViewHolder(guestsView);
break;
case VIEW_TYPE_BUDDY:
layout = R.layout.row_all_members;
View buddyView = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new BuddyViewHolder(buddyView);
break;
default:
viewHolder=null;
break;
}
return viewHolder;
}
// First ViewHolder of object type Call
// Reference to the views for each call items to display desired information
public class MemberViewHolder extends RecyclerView.ViewHolder {
private TextView tv_member_name,tv_member_id;
private ImageView iv_close;
public MemberViewHolder(View itemView) {
super(itemView);
// Initiate view
tv_member_name=(TextView)itemView.findViewById(R.id.tv_member_name);
tv_member_id=(TextView)itemView.findViewById(R.id.tv_member_id);
iv_close=(ImageView)itemView.findViewById(R.id.iv_close);
}
public void showMemberDetails(MemberList mem){
// Attach values for each item
String memName = mem.getMemberName();
String memid = mem.getMemberID();
tv_member_name.setText(memName);
tv_member_id.setText(memid);
iv_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
try {
modifyListData.remove(position);
notifyItemRemoved(position);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
}
}
// Second ViewHolder of object type SMS
// Reference to the views for each call items to display desired information
public class GuestViewHolder extends RecyclerView.ViewHolder {
private TextView tv_guest_name,tv_guestType;
private ImageView iv_close;
public GuestViewHolder(View itemView) {
super(itemView);
// Initiate view
tv_guest_name =(TextView)itemView.findViewById(R.id.tv_guest_name);
tv_guestType =(TextView)itemView.findViewById(R.id.tv_guestType);
iv_close=(ImageView)itemView.findViewById(R.id.iv_close);
}
public void showGuestDetails(GuestList guest){
// Attach values for each item
String guestName = guest.getGuestName();
tv_guest_name.setText(guestName);
}
}
// Second ViewHolder of object type SMS
// Reference to the views for each call items to display desired information
public class BuddyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_member_name,tv_member_id;
private ImageView iv_close;
public BuddyViewHolder(View itemView) {
super(itemView);
// Initiate view
tv_member_name =(TextView)itemView.findViewById(R.id.tv_member_name);
tv_member_id=(TextView)itemView.findViewById(R.id.tv_member_id);
iv_close=(ImageView)itemView.findViewById(R.id.iv_close);
}
public void showBuddyDetails(BuddyList buddy){
// Attach values for each item
String buddyName = buddy.getMemberName();
String memid = buddy.getMemberID();
tv_member_name.setText(buddyName);
tv_member_id.setText(memid);
}
}
}
这是活动中的适配器调用或设置代码:
linearLayoutManager=new LinearLayoutManager(this);
recyclerviewModify.setLayoutManager(linearLayoutManager);
eventModifyAdapter =new EventModifyAdapter(EventRegistrationActivity.this);
recyclerviewModify.setAdapter(eventModifyAdapter);
eventModifyAdapter.setEventModifyData(finalModifyList);
eventModifyAdapter.notifyDataSetChanged();
请帮助我解决该错误,因为在logcat中没有任何线索表明实际错误来自何处。 欢迎提出所有建议。
答案 0 :(得分:3)
我遇到了这个问题,因为我调用了onCreateViewHolder方法并返回null,导致Recycleview类的createviewholder方法中的holder变量为null。因此,使方法oncreateviewholder的返回值不为空。
答案 1 :(得分:0)
我遇到过同样的问题。我使用了三个视图持有者来保存三种类型的元素。 原来getItemViewType(int position)中存在问题。
只需确保此方法返回的整数始终是您定义的视图类型之一。
请确保如果您使用过开关盒内的默认值:从未使用过。
答案 2 :(得分:0)
删除它,它将正常工作:
default:
viewHolder=null;
break;
如果您的onCreateViewHolder
方法返回null
,则可能会发生这种情况。