报错信息:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
错误代码:
// 绑定展示姓名和邮箱的文本框
private final TextView nameTextView = findViewById(R.id.name);
private final TextView emailTextView = findViewById(R.id.email);
错误原因:
在Activity布局文件加载前就绑定控件,会因控件尚未加载而无法绑定,产生空指针异常。
修改:
// 绑定展示姓名和邮箱的文本框
private TextView nameTextView;
private TextView emailTextView;
// onCreate中
nameTextView = findViewById(R.id.name);
emailTextView = findViewById(R.id.email);
不要再犯加载前就绑定的错误了!
一杯茶,一包烟,一个bug修一天