Android - Sharedpreferences Not Working?
Solution 1:
You are receiving this error because you are writing to a different SharedPreferences
than you are reading from. If you are using default SharedPreferences
, make sure you read and write to the default preferences.
// Read the String from SharedPreferencesStringawesomeString= PreferenceManager.getDefaultSharedPreferences(context).getString("key", "");
// Write the String to SharedPreferences
PreferenceManager.getDefaultSharedPreferences(context)
.edit().putString("key", awesomeString).commit();
Solution 2:
That's because you don't read and write the preferences in the same preference file.
To write you use:
SharedPreferencespreferences= getSharedPreferences(getPref, Context.MODE_PRIVATE);
To read you use:
PreferenceManager.getDefaultSharedPreferences(this);
So you write you preferences in a file getPref
(which is null according to your code, don't you get any error?) but you read your preferences in the default file provided by the Android API.
Solution 3:
Are you sure that you put any value in rfid? Here
publicvoidrun() {
Toast.makeText(MainActivity2.this,"Login Success", Toast.LENGTH_SHORT).show();
editor.putString("rfid" ,rfid);
editor.commit();
}
I think rfid
is null
. I did not find any place where you are initializing it.
Put rfid
in toast or log it and you will see.
And as everybody else say you have to use default shared preferences in both classes. Here you can find why Difference between getDefaultSharedPreferences and getSharedPreferences
Post a Comment for "Android - Sharedpreferences Not Working?"