Skip to content Skip to sidebar Skip to footer

Sharedpreferences.getstring() Return Null

Im trying to display the user email in a textView using SharedPreferences. Shared preferences is created in loginActivity. I try to access it from mainActivity. My session using sh

Solution 1:

to read the stored preferences you need to do:

to save

SharedPreferencesspref= getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editoreditor= spref.edit();
editor.putString("myTextViewValue", prefVal); //
editor.commit();

to read it

SharedPreferencespreferences= getPreferences(Activity.MODE_PRIVATE);
StringstoredPreference= preferences.getStr("myTextViewValue", null);

Solution 2:

This happens because your value is not stored in the shared preferences.

SharedPreferencespref= getSharedPreferences("your Pref Name", 0) // 0 for Private ModeStringname= pref.getString("your key store when login", null); // null is the default value you can put it here "No value". then you will not get null pointer.

Post a Comment for "Sharedpreferences.getstring() Return Null"