因此,MainActivity启动后出现此错误。
04-04 17:30:37.041 7835-7835/net.example.ricardo.tcc2 E/AndroidRuntime: FATAL EXCEPTION: main
04-04 17:30:37.041 7835-7835/net.example.ricardo.tcc2 E/AndroidRuntime: Process: net.example.ricardo.tcc2, PID: 7835
04-04 17:30:37.041 7835-7835/net.example.ricardo.tcc2 E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {net.example.ricardo.tcc2/org.example.ricardo.tcc2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
04-04 17:30:37.041 7835-7835/net.example.ricardo.tcc2 E/AndroidRuntime: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4008)
04-04 17:30:37.041 7835-7835/net.example.ricardo.tcc2 E/AndroidRuntime: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4039)
使用SharedPreferences
存储的Facebook ID和名称。
onCreate我有这个:
SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);
nameStr = sharedPrefs.getString("name", null);
idStr = sharedPrefs.getString("id", null);
onResume我有这个:
public void onResume(){
super.onResume();
username = (TextView) findViewById(R.id.username);
profileImage = (ProfilePictureView) findViewById(R.id.fotoPerfil);
if (nameStr != null && idStr != null) {
sessaoIniciada();
}
}
有什么想法吗?
从“字符串”首选项中获取字符串时,需要KEY和默认值。默认值不能为null,因此将其设置为空字符串或一些随机字符串
像这样修改:
SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);
nameStr = sharedPrefs.getString("name", "empty");
idStr = sharedPrefs.getString("id", "empty");
希望能帮助到你!!!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句