任何时候我尝试从SharedPreference
开始getInt()
我的应用程序崩溃,但我可以通过偏好作为地图进行迭代。例如,请参见下面带星行:尽管存在值,但无法获得共享偏好值
private void loadPref(){
myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int sf = DEFAULT_VALUE;
Map<String,?> keys = myPrefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
if (entry.getKey().contentEquals("score_format"))
// this works: //*****
sf = Integer.parseInt(entry.getValue().toString()); //*****
}
// but this does not: //*****
// sf = myPrefs.getInt("score_format", DEFAULT_VALUE); //*****
setScoreFormat(sf);
}
很显然,我的首选项被保存(此采样并在多项活动中工作的优先选择屏幕证明)。我在尝试访问getDefaultSharedPreferences之前调用super.onCreate()。
我应该考虑了解为什么此代码无法正常工作?为什么地图可以工作,但不是“getInt”方法?我确实注意到,如果我试图明确地转换键值,应用程序也会崩溃......我必须先将它转换为String。
我错过了什么?
你可以发布异常的logcat吗?和'DEFAULT_VALUE'的定义? – Szymon