I want to load the user preferences when I start the application. The preferences are correctly stored, because when I start the PreferenceActivity from the main activity it will load the saved value. The problem is that in the main activity I'm not able to load the preferences with this method:
private void updateFromPreferences() {
Context context = getApplicationContext();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
depAdd = prefs.getString(Preferences.PREF_DEP_ADD, "");
arrAdd = prefs.getSt开发者_开发百科ring(Preferences.PREF_ARR_ADD, "");
}
Is there something wrong?
Use a public static final String so you will always access the right/same file
public static final String PREFS_FILE = "MyPrefs";
Create new SharedPreferences object
SharedPreferences sharedpreferences = getSharedPreferences(PREFS_FILE, 0);
Get whatever value you want from the preferences file
depAdd = sharedpreferences.getString(Preferences.PREF_DEP_ADD, "");
The problem was in depAdd and arrAdd that were not initializate.
精彩评论