NullPointerException 和 findViewById()
问题描述:
注意:这是针对 Stack Overflow 上常见问题的规范问题.
Note: This is intended as a canonical question for a commonly seen problem here on Stack Overflow.
我的 Android 应用程序崩溃了.我 从 logcat 中检查堆栈跟踪,发现原因是该行的 NullPointerException
My Android app crashed. I checked the stacktrace from the logcat and found that the cause is a NullPointerException
at the line
myEditText.setText(myString);
问题是什么,我该如何解决?
What is the problem and how do I fix it?
答
您没有初始化 myEditText
引用到活动中的任何 EditText
对象.
you didn't initialize myEditText
reference to any EditText
Object in the activity which holds it..
请确保您初始化它并在正确的位置使用该语句.
kindly be sure you initialize it and you use that statement in the right place.