Shared Preference Android Storing Data
I am having trouble storing data using shared preference. If I have the following code and try to run it, it crashes. I don't know why though. public class Favorites extends Activi
Solution 1:
change
SharedPreferencespref= getSharedPreferences("Preference",
MODE_WORLD_READABLE);
to
SharedPreferencespref= getSharedPreferences("Preference",
MODE_WORLD_WRITABLE);
Solution 2:
Try this, maybe it is due to null pointer exception.I am not sure.
publicclassFavoritesextendsActivity{
privatestaticfinalStringTAG_NAME="title";
privatestaticfinalStringTAG_URL="href";
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites);
Intentin= getIntent();
TextViewfavName= (TextView) findViewById(R.id.textView1);
StringFILENAME="settings";
Stringstring="hello world!";
SharedPreferencespref= getSharedPreferences("Preference",
MODE_WORLD_READABLE);
SharedPreferences.Editoreditor= pref.edit();
editor.putBoolean("keyBoolean", true);
editor.putFloat("keyFloat", 1.0f);
editor.putInt("keyInt", 1);
editor.putLong("keyLong", 1000000L);
editor.putString("keyString", "Hello Android");
editor.commit();
booleandataFromPrefBool= pref.getBoolean("keyBoolean", null);
floatdataFromPrefflaot= pref.getFloat("keyFloat", null);
intdataFromPrefInt= pref.getInt("keyInt", null);
longdataFromPrefLong= pref.getLong("keyLong", null);
StringdataFromPrefString= pref.getString("keyString", null);
if(dataFromPrefInt==null)
{
favName.setText("");
}
else
{
favName.setText(dataFromPrefInt);
}
}
}
Post a Comment for "Shared Preference Android Storing Data"