firebase 如何修复以下错误:在null对象引用上?[关闭]

idv4meu8  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(158)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
昨天关门了。
Improve this question
我尝试使用Firebase进行聊天,当我尝试显示用户已提交的消息时,出现以下错误:
java.lang.NullPointerException:尝试在com.example.javalearn.Fragment.ChatFragment$2.onBindViewHolder(ChatFragment.java:99)上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'在com.example.javalearn.Fragment.ChatFragment$2.onBindViewHolder(ChatFragment.java:96)
错误发生在以下行:

if(model.getUserID().equals(mUser.getUid()))
FirebaseRecyclerAdapter<Chat, ChatMyViewHolder> adapter = new FirebaseRecyclerAdapter<Chat, ChatMyViewHolder>(options)

下面是用于加载消息的代码:
ChatFragment.java

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_chat, container, false);
    messageRecycleView = view.findViewById(R.id.chatMessageListRv);
    messageRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
    editTextInputMessages = view.findViewById(R.id.chatInputMessageEt);
    imageButtonSendMessage = view.findViewById(R.id.chatSendMessageImgBtn);
    circleImageViewUserProfilePic = view.findViewById(R.id.chatUserProfileImageAppBar);
    textViewUserName = view.findViewById(R.id.chatUserProfileNameAppBar);

    Bundle args = getArguments();
    OtherUserId = args.get("userid").toString();

    mUserRef = FirebaseDatabase.getInstance().getReference().child("users");
    messageRef = FirebaseDatabase.getInstance().getReference().child("Messages");

    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();

    LoadOtherUser();
    LoadMessage();

    imageButtonSendMessage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SendMessage();
        }
    });
    return view;
}

加载消息的方法:

private void LoadMessage() {
        FirebaseRecyclerOptions<Chat> options = new FirebaseRecyclerOptions.Builder<Chat>().setQuery(messageRef.child(mUser.getUid()).child(OtherUserId), Chat.class).build();
        FirebaseRecyclerAdapter<Chat, ChatMyViewHolder> adapter = new FirebaseRecyclerAdapter<Chat, ChatMyViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull ChatMyViewHolder holder, int position, @NonNull Chat model) {
                if(model.getUserID().equals(mUser.getUid())){
                    holder.friendProfilePic.setVisibility(View.GONE);
                    holder.friendMessage.setVisibility(View.GONE);
                    holder.myProfilePic.setVisibility(View.VISIBLE);
                    holder.myMessage.setVisibility(View.VISIBLE);

                    holder.myMessage.setText(model.getMessage());
                } else {

                    holder.friendProfilePic.setVisibility(View.VISIBLE);
                    holder.friendMessage.setVisibility(View.VISIBLE);
                    holder.myProfilePic.setVisibility(View.GONE);
                    holder.myMessage.setVisibility(View.GONE);

                    holder.friendMessage.setText(model.getMessage());
                    Picasso.get().load(OtherUserImageUrl).placeholder(R.drawable.ic_baseline_person_24).into(holder.friendProfilePic);
                }

            }

            @NonNull
            @Override
            public ChatMyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_messagetext, parent, false);

                return new ChatMyViewHolder(view);
            }
        };
        adapter.startListening();
        messageRecycleView.setAdapter(adapter);
    }

聊天类:

public class Chat {
    public String message, status, userID;

    public Chat(){

    }

    public Chat(String message, String status, String userID) {
        this.message = message;
        this.status = status;
        this.userID = userID;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }
}

聊天的ViewHolder:

public class ChatMyViewHolder extends RecyclerView.ViewHolder {

    public CircleImageView friendProfilePic, myProfilePic;
    public TextView friendMessage, myMessage;
    public ChatMyViewHolder(@NonNull View itemView) {
        super(itemView);

        friendProfilePic = itemView.findViewById(R.id.friendMessageProfileImageCiv);
        myProfilePic = itemView.findViewById(R.id.yourMessageProfileImageCiv);
        friendMessage = itemView.findViewById(R.id.friendMessageTv);
        myMessage = itemView.findViewById(R.id.yourMessageTv);

    }
}
kkbh8khc

kkbh8khc1#

这是你想要的输出吗?

如果是,这里是布局listitem_messagetext. xml的更改代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/friendMessageProfileImageCiv"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/flower" />

    <TextView
        android:id="@+id/friendMessageTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="8dp"
        android:text="This is text"
        android:textColor="@color/black"
        android:textSize="16dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:orientation="horizontal"
    android:padding="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/yourMessageTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="8dp"
        android:text="This is text"
        android:textColor="@color/black"
        android:textSize="16dp" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/yourMessageProfileImageCiv"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/flower" />
</LinearLayout>

相关问题