为什么我在FirebaseRecyclerAdapter viewHolder上遇到java.lang.NullPointerException?

时间:2018-03-20 23:26:08

标签: java android android-recyclerview nullpointerexception dialogflow

我正在尝试使用Android Studio在Android上运行dialogFlow聊天应用。

我似乎在我认为的viewHolder变量上得到了NullPointerException。

我对Android开发相对较新,所以对我来说很容易。

这是我的适配器的代码:

adapter = new FirebaseRecyclerAdapter<ChatMessage, ChatRec>(ChatMessage.class,R.layout.activity_message_list,ChatRec.class,mDatabase.child("chat")) {
        @Override
        protected void populateViewHolder(ChatRec viewHolder, ChatMessage model, int position) {

            if (model.getMsgUser().equals("user")) {

                viewHolder.rightText.setText(model.getMsgText());

                viewHolder.rightText.setVisibility(View.VISIBLE);
                viewHolder.leftText.setVisibility(View.GONE);
            }
            else {
                viewHolder.leftText.setText(model.getMsgText());

                viewHolder.rightText.setVisibility(View.GONE);
                viewHolder.leftText.setVisibility(View.VISIBLE);
            }
        }
    };

这是错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.algonquincollege.smyt0058.oso.ChatActivity$3.populateViewHolder(ChatActivity.java:166)
at com.algonquincollege.smyt0058.oso.ChatActivity$3.populateViewHolder(ChatActivity.java:159)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:218)                                                   
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)

另外,您可能需要查看我的ChatMessage.java模型:

public class ChatMessage {

  private String msgText;
  private String msgUser;

  public ChatMessage(String msgText, String msgUser){
    this.msgText = msgText;
    this.msgUser = msgUser;
  }

  public ChatMessage() {}

  public String getMsgText() {
    return msgText;
  }

  public void setMsgText(String msgText) {
    this.msgText = msgText;
  }

  public String getMsgUser() {
    return msgUser;
  }

  public void setMsgUser(String msgUser) {
    this.msgUser = msgUser;
  }
}

我的ChatRec.java ViewHolder类:

public class ChatRec extends RecyclerView.ViewHolder {

  TextView leftText,rightText;

  public ChatRec(View itemView){
    super(itemView);

    leftText = itemView.findViewById(R.id.leftText);
    rightText = itemView.findViewById(R.id.rightText);
  }
}

我的msg_list.xml显示了我的rightText和leftText

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/leftText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_alignParentStart="true"
    android:text="Hello this is me!!"
    android:padding="8dp"
    android:textColor="#212121"
    android:background="@drawable/left_background"
    android:elevation="2dp"
    />


<TextView
    android:id="@+id/rightText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_alignParentEnd="true"
    android:text="Hi!! How are you!!"
    android:background="@drawable/right_background"
    android:textColor="#fff"
    android:padding="8dp"
    android:elevation="2dp"/>

1 个答案:

答案 0 :(得分:-1)

.setText(model.getMsgText());是空的。这就是你得到错误的原因。首先,你应该将变量设置为你的模型项,然后你可以使用getmodel-item。