Android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null..空指針問題,費勁心思才找到報空指針的原因:
代碼是這樣的,有多種布局類型
@Override public int getItemViewType(int position) { BmobIMMessage message = msgs.get(position); if(message.getMsgType().equals(BmobIMMessageType.IMAGE.getType())){ return message.getFromId().equals(currentUid) ? TYPE_SEND_IMAGE: TYPE_RECEIVER_IMAGE; }else if(message.getMsgType().equals(BmobIMMessageType.LOCATION.getType())){ return message.getFromId().equals(currentUid) ? TYPE_SEND_LOCATION: TYPE_RECEIVER_LOCATION; }else if(message.getMsgType().equals(BmobIMMessageType.VOICE.getType())){ return message.getFromId().equals(currentUid) ? TYPE_SEND_VOICE: TYPE_RECEIVER_VOICE; }else if(message.getMsgType().equals(BmobIMMessageType.TEXT.getType())){ return message.getFromId().equals(currentUid) ? TYPE_SEND_TXT: TYPE_RECEIVER_TXT; }else if(message.getMsgType().equals(BmobIMMessageType.VIDEO.getType())){ return message.getFromId().equals(currentUid) ? TYPE_SEND_VIDEO: TYPE_RECEIVER_VIDEO; }else if(message.getMsgType().equals("agree")) {//顯示歡迎 return TYPE_AGREE; }else{ // return -1; return TYPE_AGREE; }
然后這是
getItemCount()
@Override public int getItemCount() { return msgs.size(); }
最后發現是因為itemCount與viewType不匹配導致的
例如:
recyclerview總共有6種類型,現在只寫了一種類型測試,其他的都沒有些,才導致報空,然后在getItemCount中return 1; 就ok了