Error:Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSeq

I encountered an error while studying Android development: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Reason: The variable you defined is not recognized, he doesn't know which one it is. For example, mine is because there are two TextViews, and the system does not know how to use them, which causes the program to fail to run.
My xml has two Textview
insert image description here

Solution: Find the variable we need to use and define it clearly.

insert image description here
Summary: When we need to use a large range of variables, we need to be precise, determine which variable we want to use, and assign it an accurate value, so that we can ensure that our code runs correctly.

Guess you like

Origin blog.csdn.net/weixin_51961968/article/details/127144166