所以我得到这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我运行了调试工具,这些行为空:
TextView leftMessageView = (TextView) row.findViewById(R.id.leftmsgr);
leftMessageView.setText(Servermessage);
此函数的来源:
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View row;
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ChMessage chMessageObj = getItem(position);
if (chMessageObj.left) {
row = inflater.inflate(R.layout.leftmessage, parent, false);
}else{
row = inflater.inflate(R.layout.rightmessage, parent, false);
}
TextView rightMessageView = (TextView) row.findViewById(R.id.rightmsgr);
TextView leftMessageView = (TextView) row.findViewById(R.id.leftmsgr);
rightMessageView.setText(chMessageObj.message);
leftMessageView.setText(Servermessage);
return row;
}
不确定为什么它不为null?
任何帮助都会很棒!
谢谢
答案 0 :(得分:0)
尝试使用以下代码修复NullPointerException
:
public View getView(int position, View convertView, @NonNull ViewGroup parent)
{
View row;
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ChMessage chMessageObj = getItem(position);
if (chMessageObj.left) {
row = inflater.inflate(R.layout.leftmessage, parent, false);
}else{
row = inflater.inflate(R.layout.rightmessage, parent, false);
}
TextView rightMessageView = (TextView) row.findViewById(R.id.rightmsgr);
TextView leftMessageView = (TextView) row.findViewById(R.id.leftmsgr);
if (rightMessageView!=null) {
rightMessageView.setText(chMessageObj.message);
}
if (leftMessageView!=null) {
leftMessageView.setText(Servermessage);
}
return row;
}
然后,如果TextView为null是正确的,那么我不知道。检查您的XML布局。希望对您有所帮助。