【发布时间】:2014-05-06 20:50:08
【问题描述】:
我正在使用首选项在我的 Nexus 7 应用中保存一些用户设置。我将值保存到首选项的代码是:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String systemId = spinnerActivity.getSelectedItem().toString();
editor.putString(PreferenceKeys.SAVED_SYSTEMID, systemId);
if (!editor.commit()) {
Toast.makeText(getApplicationContext(), "Error saving System ID", Toast.LENGTH_LONG).show();
}
我已经用调试器逐步完成了这个,它被正确调用了。当我重新启动我的应用程序时。并尝试使用下面的代码读回该值,我总是得到空值。
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String savedSystemId = sharedPref.getString(PreferenceKeys.SAVED_SYSTEMID, null);
ConnectionInfo.setSystemId(savedSystemId);
加载是从主活动中的 onCreate() 函数调用的。奇怪的是,在应用程序的其他地方加载了其他偏好值。工作正常,只是这一种情况不起作用。任何人都可以看到有什么问题吗?
【问题讨论】:
-
您确定要保存您认为的价值吗?
-
是的,我已经非常仔细地在调试器中逐步完成了,null 值永远不会保存。我已经将它保存了一个有效值。在重新启动应用程序时。然而,我总是读回 null。
标签: android android-preferences