我想保存一個共享參數,然後重新啟動應用程序。但是它並不能保存,也沒加載正確的值。代碼是這樣的:
int punkteint = 0;
TextView test1;
String points;
private static String SHARED_PREF_ID = "0";
public void onCreate(Bundle savedInstanceState) {
test1 = (TextView) findViewById(R.id.test1);
SharedPreferences load = getSharedPreferences(SHARED_PREF_ID, 0);
points = load.getString("punkte", "0");
points = SHARED_PREF_ID;
test1.setText(points);
mehrPunkte();
}
public void mehrPunkte() {
punkteint++;
SHARED_PREF_ID = Integer.toString(punkteint);
SharedPreferences load = getSharedPreferences(SHARED_PREF_ID, MODE_PRIVATE);
points = load.getString("punkte", "0");
points = SHARED_PREF_ID;
test1.setText(points);
SharedPreferences save = getSharedPreferences(SHARED_PREF_ID, MODE_PRIVATE);
save.edit().putString("punkte", SHARED_PREF_ID).commit();
}
出現的錯誤請求指點,謝謝!
SharedPreferences.Editor prefEditor = save.edit();
prefEditor.putString("blah", blah);
PredEditor.commit()
在onPause保存,在onResume上加載.