Ricardo Caldas
Ricardo Caldas

Reputation: 51

SharedPreferences Facebook

So i am getting this errors after the MainActivity start.

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)

Using SharedPreferences to store facebook id and name.

onCreate i have this:

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

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

onResume i have this:

public void onResume(){
        super.onResume();
        username = (TextView) findViewById(R.id.username);
        profileImage = (ProfilePictureView) findViewById(R.id.fotoPerfil);


        if (nameStr != null && idStr != null) {
            sessaoIniciada();
        }
    }

Any thoughts?

Upvotes: 2

Views: 59

Answers (1)

Kostas Drak
Kostas Drak

Reputation: 3260

When you are getting a string from the String preferences you need the KEY and a default value. The default value cannot be null so set it to empty string or some random string

Modify like this:

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

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

Hope it helps!!!

Upvotes: 1

Related Questions