RecyclerView如何针对NullPointerException出错?

时间:2016-09-28 04:42:36

标签: nullpointerexception android-recyclerview

DES  当我设置 TYPE_RECEIVE_IMAGE 数据集的类型,然后说出这个错误,这是一个运行时错误,我找不到问题,帮帮我!

当我设置 TYPE_RECEIVE_IMAGE 数据集的类型,然后说出这个错误,这是一个运行时错误,我找不到问题,帮帮我吧! 错误消息

  

java.lang.NullPointerException at   android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:5780)                                                                            在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5003)                                                                            在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4913)                                                                            在   android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2029)                                                                            在   android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)                                                                            在   android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)                                                                            在   android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)                                                                            在   android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)                                                                            在   android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)                                                                            在   android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1505)                                                                            在   android.support.v7.widget.RecyclerView.access $ 400(RecyclerView.java:151)                                                                            在android.support.v7.widget.RecyclerView $ 1.run(RecyclerView.java:305)                                                                            在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)                                                                            在android.view.Choreographer.doCallbacks(Choreographer.java:574)                                                                            在android.view.Choreographer.doFrame(Choreographer.java:543)                                                                            在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)                                                                            在android.os.Handler.handleCallback(Handler.java:733)                                                                            在android.os.Handler.dispatchMessage(Handler.java:95)                                                                            在android.os.Looper.loop(Looper.java:136)                                                                            在android.app.ActivityThread.main(ActivityThread.java:5052)                                                                            at java.lang.reflect.Method.invokeNative(Native Method)                                                                            在java.lang.reflect.Method.invoke(Method.java:515)                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:796)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)                                                                            在dalvik.system.NativeStart.main(本地方法)

以下是我的代码:适配器

public class ChatRecordAdapter extends    RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int RECEIVE_MSG = 1;
private static final int SEND_MSG = 2;

public static final int TYPE_SEND_TXT = 3;
public static final int TYPE_RECEIVE_TXT = 4;
public static final int TYPE_SEND_VOICE = 5;
public static final int TYPE_RECEIVE_VOICE = 6;
public static final int TYPE_SEND_FILE = 7;
public static final int TYPE_RECEIVE_FILE = 8;
public static final int TYPE_SEND_IMAGE = 9;
public static final int TYPE_RECEIVE_IMAGE = 10;
public static final int TYPE_SEND_LOCATION = 11;
public static final int TYPE_RECEIVE_LOCATION = 12;
public static final int TYPE_SEND_VIDEO = 13;
public static final int TYPE_RECEIVE_VIDEO = 14;
public static final int TYPE_SEND_CMD = 15;
public static final int TYPE_RECEIVE_CMD = 16;
private List<ChatModel> messages;
private ChatViewClickListener chatViewClickListener;
private ChatViewLongClickListener chatViewLongClickListener;
private ChatViewHeaderClickListener chatViewHeaderClickListener;

public void setChatViewHeaderClickListener(ChatViewHeaderClickListener chatViewHeaderClickListener) {
    this.chatViewHeaderClickListener = chatViewHeaderClickListener;
}

public void setViewLongClickListener(ChatViewLongClickListener chatViewLongClickListener) {
    this.chatViewLongClickListener = chatViewLongClickListener;
}

public void setViewClickListener(ChatViewClickListener chatViewClickListener) {
    this.chatViewClickListener = chatViewClickListener;
}

public ChatRecordAdapter(List<ChatModel> messages) {
    this.messages = messages;
}

@Override
public int getItemViewType(int position) {
    if (messages != null) {
        ChatModel chatModel = messages.get(position);
        if (chatModel != null) {
            Im.MSG_CONTENT_TYPE msgType = chatModel.getMsgType();
            boolean receiveMsg = chatModel.isReceiveMsg();
            if (Im.MSG_CONTENT_TYPE.TYPE_TXT == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_TXT;
                } else {
                    return TYPE_SEND_TXT;
                }
            } else if (Im.MSG_CONTENT_TYPE.TYPE_VOICE == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_VOICE;
                } else {
                    return TYPE_SEND_VOICE;
                }
            } else if (Im.MSG_CONTENT_TYPE.TYPE_FILE == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_FILE;
                } else {
                    return TYPE_SEND_FILE;
                }
            } else if (Im.MSG_CONTENT_TYPE.TYPE_IMAGE == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_IMAGE;
                } else {
                    return TYPE_SEND_IMAGE;
                }
            } else if (TYPE_LOCATION == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_LOCATION;
                } else {
                    return TYPE_SEND_LOCATION;
                }
            } else if (Im.MSG_CONTENT_TYPE.TYPE_VIDEO == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_VIDEO;
                } else {
                    return TYPE_SEND_VIDEO;
                }
            } else if (Im.MSG_CONTENT_TYPE.TYPE_CMD == msgType) {
                if (receiveMsg) {
                    return TYPE_RECEIVE_CMD;
                } else {
                    return TYPE_SEND_CMD;
                }
            }
        }
    }
    return super.getItemViewType(position);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LogManager.d("REEEEEE", "onCreateViewHolder");
    if (viewType == TYPE_SEND_TXT) {
        ChatItemTextSendBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_text_send, parent, false);
        ChatTextSendHolder holder = new ChatTextSendHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.sendLl.setVisibility(View.GONE);
        return holder;
    } else if (viewType == TYPE_RECEIVE_TXT) {
        ChatItemTextReceiveBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_text_receive, parent, false);
        ChatTextReceiveHolder holder = new ChatTextReceiveHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.receiveLl.setVisibility(View.GONE);
        return holder;

    } else if (viewType == TYPE_SEND_VOICE) {
        ChatItemVoiceSendBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_voice_send, parent, false);
        ChatVoiceSendHolder holder = new ChatVoiceSendHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.sendLl.setVisibility(View.GONE);
        return holder;

    } else if (viewType == TYPE_RECEIVE_VOICE) {
        ChatItemVoiceReceiveBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_voice_receive, parent, false);
        ChatVoiceReceiveHolder holder = new ChatVoiceReceiveHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.receiveLl.setVisibility(View.GONE);
        return holder;
    } else if (viewType == TYPE_RECEIVE_IMAGE) {
        ChatItemImageReceiveBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_image_receive, parent, false);
        ChatItemImageReceiveHolder holder = new ChatItemImageReceiveHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.receiveLl.setVisibility(View.GONE);

    } else if (viewType == TYPE_SEND_IMAGE) {
        ChatItemImageSendBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_image_send, parent, false);
        ChatItemImageSendHolder holder = new ChatItemImageSendHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.sendLl.setVisibility(View.GONE);
    } else if (viewType == TYPE_RECEIVE_FILE) {

        ChatItemFileReceiveBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_file_receive, parent, false);
        ChatItemFileReceiveHolder holder = new ChatItemFileReceiveHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.receiveLl.setVisibility(View.GONE);
    } else if (viewType == TYPE_SEND_FILE) {
        ChatItemFileSendBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_item_file_send, parent, false);
        ChatItemFileSendHolder holder = new ChatItemFileSendHolder(binding.getRoot());
        holder.setBinding(binding);
        binding.sendLl.setVisibility(View.GONE);
    } else if (viewType == TYPE_RECEIVE_LOCATION) {

    } else if (viewType == TYPE_SEND_LOCATION) {

    } else if (viewType == TYPE_RECEIVE_VIDEO) {

    } else if (viewType == TYPE_SEND_VIDEO) {

    } else if (viewType == TYPE_RECEIVE_CMD) {

    } else if (viewType == TYPE_SEND_CMD) {

    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    LogManager.d("REEEEEE","onBindViewHolder");
    ChatModel chatModel = messages.get(position);
    String userName = chatModel.getUserName();
    if (chatModel != null) {
        if (holder instanceof ChatTextSendHolder || holder instanceof ChatTextReceiveHolder) {
            ChatTextModel chatTextModel = (ChatTextModel) chatModel;
            if (holder instanceof ChatTextSendHolder) {
                ChatItemTextSendBinding binding = ((ChatTextSendHolder) holder).getBinding();
                binding.setViewModel(chatTextModel);
                bind(position,TYPE_SEND_TXT, chatModel, binding.content);
                bindHeaderClick(binding.header,userName);
            } else if (holder instanceof ChatTextReceiveHolder) {
                ChatItemTextReceiveBinding binding = ((ChatTextReceiveHolder) holder).getBinding();
                binding.setViewModel(chatTextModel);
                bind(position,TYPE_RECEIVE_TXT, chatModel, binding.content);
                bindHeaderClick(binding.header,userName);
            }


        } else if (holder instanceof ChatVoiceReceiveHolder || holder instanceof ChatVoiceSendHolder) {

            ChatVoiceModel chatTextModel = (ChatVoiceModel) chatModel;
            if (holder instanceof ChatVoiceReceiveHolder) {
                ChatItemVoiceReceiveBinding binding = ((ChatVoiceReceiveHolder) holder).getBinding();
                binding.setViewModel(chatTextModel);
                bind(position,TYPE_RECEIVE_VOICE, chatModel, binding.content);
                bindHeaderClick(binding.header,userName);
            } else if (holder instanceof ChatVoiceSendHolder) {
                ChatItemVoiceSendBinding binding = ((ChatVoiceSendHolder) holder).getBinding();
                binding.setViewModel(chatTextModel);
                bind(position,TYPE_SEND_VOICE, chatModel, binding.content);
                bindHeaderClick(binding.header,userName);
            }

        }else if (holder instanceof ChatItemImageReceiveHolder || holder instanceof ChatItemImageSendHolder){
            ChatPicModel chatPicModel = (ChatPicModel) chatModel;
            if (holder instanceof ChatItemImageReceiveHolder) {
                ChatItemImageReceiveBinding binding = ((ChatItemImageReceiveHolder) holder).getBinding();
                binding.setViewModel(chatPicModel);
                bind(position,TYPE_RECEIVE_IMAGE, chatPicModel, binding.content);
                bindHeaderClick(binding.header,userName);
            } else if (holder instanceof ChatItemImageSendHolder) {
                ChatItemImageSendBinding binding = ((ChatItemImageSendHolder) holder).getBinding();
                binding.setViewModel(chatPicModel);
                bind(position,TYPE_SEND_IMAGE, chatPicModel, binding.content);
                bindHeaderClick(binding.header,userName);
            }
        }else if (holder instanceof ChatItemFileReceiveHolder || holder instanceof ChatItemFileSendHolder){
            ChatFileModel chatFileModel = (ChatFileModel) chatModel;
            if (holder instanceof ChatItemFileReceiveHolder) {
                ChatItemFileReceiveBinding binding = ((ChatItemFileReceiveHolder) holder).getBinding();
                binding.setViewModel(chatFileModel);
                bind(position,TYPE_RECEIVE_FILE, chatFileModel, binding.content);
                bindHeaderClick(binding.header,userName);
            } else if (holder instanceof ChatItemFileSendHolder) {
                ChatItemFileSendBinding binding = ((ChatItemFileSendHolder) holder).getBinding();
                binding.setViewModel(chatFileModel);
                bind(position,TYPE_SEND_FILE, chatFileModel, binding.content);
                bindHeaderClick(binding.header,userName);
            }
        }
    } else {

    }

}

private void bind(final int position, final int type, final ChatModel chatModel, View view) {
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (chatViewClickListener != null) {
                chatViewClickListener.onViewClick(view,type,chatModel);
            }
        }
    });
    binChatItemLongClick(position,type,chatModel,view);
}
private void binChatItemLongClick(final int position,final int type, final ChatModel chatModel, final View view){
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (chatViewLongClickListener !=null){
                chatViewLongClickListener.onViewLongClick(position,type,view,chatModel);
                return true;
            }
            return false;
        }
    });
}
private void bindHeaderClick(ImageView view , final String username){
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (chatViewHeaderClickListener!=null){
                chatViewHeaderClickListener.onHeaderImageClick(username);
            }
        }
    });
}
@Override
public int getItemCount() {
    return messages == null ? 0 : messages.size();
}

public List<ChatModel> getMessages() {
    return messages;
}

public class ChatTextReceiveHolder extends RecyclerView.ViewHolder {
    private ChatItemTextReceiveBinding binding;

    public ChatTextReceiveHolder(View itemView) {
        super(itemView);
    }

    public ChatItemTextReceiveBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemTextReceiveBinding binding) {
        this.binding = binding;
    }
}

public class ChatTextSendHolder extends RecyclerView.ViewHolder {
    private ChatItemTextSendBinding binding;

    public ChatTextSendHolder(View itemView) {
        super(itemView);
    }

    public ChatItemTextSendBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemTextSendBinding binding) {
        this.binding = binding;
    }
}

public class ChatVoiceSendHolder extends RecyclerView.ViewHolder {
    private ChatItemVoiceSendBinding binding;

    public ChatVoiceSendHolder(View itemView) {
        super(itemView);
    }

    public ChatItemVoiceSendBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemVoiceSendBinding binding) {
        this.binding = binding;
    }
}

public class ChatVoiceReceiveHolder extends RecyclerView.ViewHolder {
    private ChatItemVoiceReceiveBinding binding;

    public ChatVoiceReceiveHolder(View root) {
        super(root);
    }

    public ChatItemVoiceReceiveBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemVoiceReceiveBinding binding) {
        this.binding = binding;
    }
}

public class ChatItemImageReceiveHolder extends RecyclerView.ViewHolder

{
    private ChatItemImageReceiveBinding binding;
    public ChatItemImageReceiveHolder(View itemView) {
        super(itemView);
    }

    public ChatItemImageReceiveBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemImageReceiveBinding binding) {
        this.binding = binding;
    }
}

public class ChatItemImageSendHolder extends RecyclerView.ViewHolder{
    private ChatItemImageSendBinding binding;
    public ChatItemImageSendHolder(View itemView) {
        super(itemView);
    }

    public ChatItemImageSendBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemImageSendBinding binding) {
        this.binding = binding;
    }
}

public class ChatItemFileReceiveHolder extends RecyclerView.ViewHolder{
    private ChatItemFileReceiveBinding binding;

    public ChatItemFileReceiveHolder(View root) {
        super(root);
    }

    public ChatItemFileReceiveBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemFileReceiveBinding binding) {
        this.binding = binding;
    }
}

public class ChatItemFileSendHolder extends RecyclerView.ViewHolder{
    private ChatItemFileSendBinding binding;

    public ChatItemFileSendHolder(View itemView) {
        super(itemView);
    }

    public ChatItemFileSendBinding getBinding() {
        return binding;
    }

    public void setBinding(ChatItemFileSendBinding binding) {
        this.binding = binding;
    }
}

}

的getImage

    public void getAvtivityResult(int requestCode, int resultCode, Intent data) {
    // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
    if (resultCode != RESULT_OK)
        return;
    switch (requestCode) {
        case PhotoUtils.PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的
            Bitmap photo = data.getParcelableExtra("data");
            // 下面就是显示照片了

            break;
        }
        case PhotoUtils.CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
            String currentPicPath = data.getStringExtra("currentPicPath");
            Bitmap scaleBitmap = BitmapUtil.decodeSampledBitmapFromFile(currentPicPath, ViewUtils.dip2px(80), ViewUtils.dip2px(60));
            String cacheImagePath = ImageCacheUtils.saveBitmap(scaleBitmap);

            if (!TextUtils.isEmpty(currentPicPath)) {
                //msg, false, currentToUserName
                EMMessage msg = EMMessage.createImageSendMessage(/*currentPicPath*/cacheImagePath, false/*是否发送原图*/, currentToUserName);
                updateMsgList(msg);
            }
            break;

        }
        case PhotoUtils.FILE_SELECT_CODE:
            if (data!=null){
                Uri uri = data.getData();
                String path = IntentUtil.getPath(activity, uri);
                if (!TextUtils.isEmpty(path)){
                    EMMessage message = EMMessage.createFileSendMessage(path,currentToUserName);
                    updateMsgList(message);
                }
            }
            break;
    }
}

refreshAdapter

    public void updateMsgList(List<EMMessage> mess) {
    LogManager.d("updateMsgList");
    if (chatRecordAdapter != null) {
        List<ChatModel> messages = chatRecordAdapter.getMessages();
        if (messages != null) {
            if (mess.size() == 1) {
                updateMsgList(mess.get(0));
            } else {
                List<ChatModel> chatOnlineViewModels = convertMessages(mess);
                if (chatOnlineViewModels != null) {
                    LogManager.e("UPDATE::::", "LIST--");
                    messages.addAll(chatOnlineViewModels);
                    chatRecordAdapter.notifyDataSetChanged();
                    if (onDataChange != null) {
                        onDataChange.onChange();
                    }
                }
            }

        }
    }
}

当我设置 TYPE_RECEIVE_IMAGE 数据集的类型,然后说出这个错误,这是一个运行时错误,我找不到问题,帮帮我!

0 个答案:

没有答案