尝试在android studio中调用虚方法错误

时间:2017-01-04 13:00:16

标签: java android nullpointerexception

我开发Android应用程序并且工作正常我没有更改源代码但是今天我在模拟器上运行应用程序时遇到错误。

以下是Android Studio中的错误:

  

java.lang.NullPointerException:尝试调用虚方法   ' boolean java.lang.String.equals(java.lang.Object)'在null   对象参考                                                                      在com.facebook.misstest.MessageAdapter.getView(MessageAdapter.java:36)                                                                      在android.widget.AbsListView.obtainView(AbsListView.java:2346)                                                                      在android.widget.ListView.makeAndAddView(ListView.java:1875)                                                                      在android.widget.ListView.fillUp(ListView.java:736)                                                                      在android.widget.ListView.layoutChildren(ListView.java:1665)                                                                      在android.widget.AbsListView.onLayout(AbsListView.java:2148)                                                                      在android.view.View.layout(View.java:16630)

这是代码:

public View getView(int position, View view, ViewGroup viewGroup) {
  ChatMessage chatMessage = getItem(position);
  if (chatMessage.getMessageUserId().equals(activity.getLoggedInUserName())) {
     view = activity.getLayoutInflater().inflate(R.layout.item_out_message, viewGroup, false);
  }  else {
     view = activity.getLayoutInflater().inflate(R.layout.item_in_message, viewGroup, false);
  }

  //generating view
  populateView(view, chatMessage, position);

  return view;
}

2 个答案:

答案 0 :(得分:0)

其中一个chatMessage.getMessageUserId()activity.getLoggedInUserName()为空,请在使用equals方法之前进行检查

答案 1 :(得分:0)

可能是返回null

chatMessage.getMessageUserId() 

所以null.equals()导致此异常