我试图通过在用户按下按钮时将字符串添加到编辑器来优先保存字符串。然后我试图从首选项中检索字符串并将其转换为arrayList。
在 onCreate
this.context = getApplicationContext();
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
int size = updatableList.size();
editor.putInt("list_size", size);
for (int i = 0; i < size; i++) {
((SharedPreferences) editor).getString("list_"+i, updatableList.get(i));
}
editor.commit();
稍后在应用程序中
updatableList.add(picturePath);
i=i++;
//saving path to preference********
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
((Editor) editor).putString("list_"+i, picturePath);
editor.commit();
它说应用程序后面的首选项未使用,我认为这很奇怪,因为我认为它告诉它 putString。应用程序到达那里时崩溃。为什么我稍后在应用程序中的首选项会被使用?