简体   繁体   English

如何解决Textview setText()在空对象引用上

[英]How to solve Textview setText() is on a null object reference

So I get this error: 所以我得到这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I ran the debugging tool and these lines are null: 我运行了调试工具,这些行为空:

TextView leftMessageView = (TextView) row.findViewById(R.id.leftmsgr);
leftMessageView.setText(Servermessage);

This is the function where its coming from: 这是它来自的功能:

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;
}

Not sure why its not null? 不知道为什么它不为空?

Any Help would be great! 任何帮助将是巨大的!

Thanks 谢谢

Try to fix the NullPointerException with this code: 尝试使用以下代码修复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;
}

Then, I don't if it is correct that the TextView is null. 然后,如果TextView为null是否正确,我就不知道。 Check your XML layout. 检查您的XML布局。 Hope it helps. 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android TextView SetText null 对象引用错误 - Android TextView SetText null object reference error 如何解决对空对象引用调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39;的尝试 - How to resolve Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 如何修复 null9EB269CFDE6331CBD4B2669CFDE6331CBD5668CFDE6489DFF0BDZ668CFDE6489DFF0BDZ668CFDE6489DFF0BDZ66696 - How can I fix 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference? 带有注释的空对象引用上的setText - setText on null object reference with Annotations 在Home.onCreate上的空对象引用上无效android.widget.TextView.setText(java.lang.CharSequence)&#39; - void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at Home.onCreate 在空对象引用上使 android.widget.TextView.setText(java.lang.CharSequence)&#39; 无效 - Void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 空对象引用上的navigationView setText致命异常 - navigationView setText fatal exception on a null object reference Android应用程序中setText上的空对象引用 - Null object reference on setText in Android app 如何TextView.setText(List <String> 对象)来自List的TextView.setText(array [index])settext - how to TextView.setText(List<String>object) like TextView.setText(array[index]) settext from List Java错误:尝试在空对象引用上调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39; - Java Error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM