共享首选项Facebook

里卡多·卡尔达斯(Ricardo Caldas)

因此,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();
        }
    }

有什么想法吗?

科斯塔斯·德拉克(Kostas Drak)

从“字符串”首选项中获取字符串时,需要KEY和默认值。默认值不能为null,因此将其设置为空字符串或一些随机字符串

像这样修改:

SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);

    nameStr = sharedPrefs.getString("name", "empty");
    idStr = sharedPrefs.getString("id", "empty");

希望能帮助到你!!!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章