java.lang.NullPointerException尝试在空对象引用上调用接口方法

时间:2018-12-31 21:07:10

标签: java android android-studio object interface

我试图添加onClick ActionListener来查看持有人,我有一个内部类。我还收到以下错误

  

E / UncaughtException:java.lang.NullPointerException:尝试在空对象引用上调用接口方法OnChatSelectedListener.onChatSelected(java.lang.String)'

OnChatSelectedListener接口

public interface OnChatSelectedListener {
public void onChatSelected(String senderUsername);

}

ChatsListAdapter类:

public class ChatsListAdapter extends RecyclerView.Adapter {
private  final String TAG = this.getClass().getSimpleName();
Context context;
ArrayList<ChatMessage> mChatMessages;



OnChatSelectedListener onChatSelectedListener;



ArrayList<ChatListCardViewHolder> chatListCardViewHolders;

public class ChatListCardViewHolder extends RecyclerView.ViewHolder {

    TextView senderUsername;
    TextView briefMessageText;
    TextView timeReceived;

    public ChatListCardViewHolder(View itemView) {
        super(itemView);
        senderUsername = itemView.findViewById(R.id.sender_username);
        briefMessageText = itemView.findViewById(R.id.brief_message_text);
        timeReceived = itemView.findViewById(R.id.time_received_chats);
    }
}

public ChatsListAdapter(ArrayList<ChatMessage> chatMessages, Context context, OnChatSelectedListener onChatSelectedListener) {
    this.context = context;
    this.mChatMessages = chatMessages;
    this.onChatSelectedListener = onChatSelectedListener;
}

@NonNull
@Override
public ChatsListAdapter.ChatListCardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_chats, parent, false);



    return new ChatListCardViewHolder(v);
}


@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    final ChatsListAdapter.ChatListCardViewHolder chatListCardViewHolder = (ChatsListAdapter.ChatListCardViewHolder) holder;
    chatListCardViewHolder.senderUsername.setText(mChatMessages.get(position).getSenderUsername());
    chatListCardViewHolder.briefMessageText.setText(mChatMessages.get(position).getMessageText());
    chatListCardViewHolder.timeReceived.setText(mChatMessages.get(position).getTime());

    chatListCardViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String senderUserName = chatListCardViewHolder.senderUsername.getText().toString();
            Log.w(TAG, "Chat Clicked!!!! Chats for user: " + senderUserName);
            onChatSelectedListener.onChatSelected(senderUserName); //HERE THE PROBLEM STARTS
        }
    });


}

@Override
public int getItemCount() {
    return mChatMessages.size();
}





public ArrayList<ChatMessage> getChatMessages() {
    return mChatMessages;
}

}

我通过另一个类传递接口(试图也将其放在ChatsListAdapter类的字段中,但没有用),如下所示:

public class ChatsListFragment extends Fragment {
   OnChatSelectedListener onChatSelectedListener;

。 。 。 。 。 (更多代码与OnChatSelectedListener无关)

mAdapter =新的ChatsListAdapter(chatMessages,ChatsListFragment.this.getContext(),onChatSelectedListener);

非常感谢您的帮助。

提前谢谢!!!

0 个答案:

没有答案