sharred preference null pointer exception
//Activity-- mTweetApp.SetCaseInfo(type,teet); //storing in another class
public void SetCaseInfo(String PatientType,ArrayList arr){
// All objects are from
SharedPreferences settings =setSharedPreferences(DEALSPOT开发者_JS百科R_PREFS,0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",arr.size());
for(int i=0; i<=arr.size(); i++){
editor.putInt("Teethsselected"+String.valueOf(i), (Integer) arr.get(i));
}
editor.putString("view", PatientType);
for(int i=0; i<=arr.size(); i++){
System.out.println("Teethsselected-----"+(Integer) arr.get(i)+"type--->"+PatientType);
}
}
public void getCaseInfo() {
SharedPreferences settings =getSharedPreferences(DEALSPOTR_PREFS, 0);
int arraySize = settings.getInt("arrayLength", 0);
int teeth[] =new int[arraySize];
for(int i=0; i<arraySize; i++){
teeth[i] = ettings.getInt("Teethsselected"+String.valueOf(i),0);
}
String type =settings.getString("view"," ");
}
how to fix this?
You don't commit your changes. put editor.commit();
into your set method.
First, when you write your pref
values, no call of commit()
.
Second, the pref file is not written to /data/data/com.package.App/shared_prefs/pref_file.xml
, hence when reading the SharedPreferences
object is NULL.
SharedPreferences.Editor modifications should be commit
ed.
//Your code
editor.putString("view", PatientType);
for(int i=0; i<=arr.size(); i++){
System.out.println("Teethsselected-----"+(Integer) arr.get(i)+"type--->"+PatientType);
}
//ADD THIS
editor.commit();
SharedPreferences settings =setSharedPreferences(DEALSPOTR_PREFS,0);
instaed of above line, try using
SharedPreferences settings =getSharedPreferences(DEALSPOTR_PREFS,0);
and don't forget editor.commit()
line once the values are added..
精彩评论