所以在我正在制作的游戏中,我正在使用SharedPreferences来保存我的数据,但是它不会保存我的数据或恢复它(一个或两个)。 (是的,我知道使用double.valueOf()
是低效的,但现在不是问题)
这是我的onCreate(我获取所有已保存的数据)
public int hasPlayed = 0;
public double $money;
public int $isonmainpage = 1;
Random random = new Random();
public int $cantafford = 0;
public double $employertimer;
public double $employercounter;
public double $employeeupgrade1earnings;
public double $employeeupgrade1cost = 1;
public double $employeeupgrade1level;
public double $employerupgrade1level;
public double $employerupgrade1earnings;
public double $employerupgrade1cost = 1;
public double $allupgrades;
public String version = "0.0.1";
;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedpreferences.edit();
hasPlayed = sharedpreferences.getInt("hasPlayed", 1);
sharedpreferences.getAll();
if (hasPlayed == 1){
$money = Double.valueOf(sharedpreferences.getString("money", (Double.toString($money))));
$employercounter = Double.valueOf(sharedpreferences.getString("employercounter", (Double.toString($employercounter))));
$employerupgrade1earnings = Double.valueOf(sharedpreferences.getString("employerupgrade1earnings", (Double.toString($employerupgrade1earnings))));
$employerupgrade1level = Double.valueOf(sharedpreferences.getString("employerupgrade1level", (Double.toString($employerupgrade1level))));
$employerupgrade1cost = Double.valueOf(sharedpreferences.getString("employerupgrade1cost", (Double.toString($employerupgrade1cost))));
$employeeupgrade1level = Double.valueOf(sharedpreferences.getString("employeeupgrade1level", (Double.toString($employeeupgrade1level))));
$employeeupgrade1earnings = Double.valueOf(sharedpreferences.getString("employeeupgrade1earnings", (Double.toString($employeeupgrade1earnings))));
$employeeupgrade1cost = Double.valueOf(sharedpreferences.getString("employeeupgrade1cost", (Double.toString($employeeupgrade1cost))));
}
if (hasPlayed == 0) {
hasPlayed = 1;
editor.putInt("hasPlayed", 1);
editor.commit();
}
updatemoney();
timer();
}
这是我的onStop / onPause(当我保存数据时)
@Override
protected void onPause() {
super.onPause();
SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("money", (Double.toString($money)));
editor.putString("employercounter", (Double.toString($employercounter)));
editor.putString("employerupgrade1earnings", (Double.toString($employerupgrade1earnings )));
editor.putString("employerupgrade1level", (Double.toString($employerupgrade1level)));
editor.putString("employerupgrade1cost", (Double.toString($employerupgrade1cost)));
editor.putString("employeeupgrade1level", (Double.toString($employeeupgrade1level)));
editor.putString("employeeupgrade1earnings", (Double.toString($employeeupgrade1earnings)));
editor.putString("employeeupgrade1cost", (Double.toString($employeeupgrade1cost)));
editor.commit();
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("money", (Double.toString($money)));
editor.putString("employercounter", (Double.toString($employercounter)));
editor.putString("employerupgrade1earnings", (Double.toString($employerupgrade1earnings )));
editor.putString("employerupgrade1level", (Double.toString($employerupgrade1level)));
editor.putString("employerupgrade1cost", (Double.toString($employerupgrade1cost)));
editor.putString("employeeupgrade1level", (Double.toString($employeeupgrade1level)));
editor.putString("employeeupgrade1earnings", (Double.toString($employeeupgrade1earnings)));
editor.putString("employeeupgrade1cost", (Double.toString($employeeupgrade1cost)));
editor.commit();
}
编辑:我永远不会去了解为什么我被贬低如果我不被告知为什么我被投票。感谢
答案 0 :(得分:3)
替换
SharedPreferences savefile = getPreferences(MODE_PRIVATE);
与
SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
并关注this link
答案 1 :(得分:0)
我想应该是因为这个
hasplayed
和hasPlayed
,密钥应区分大小写
savefile.getInt("hasplayed", 0);
editor.putInt("hasPlayed", 1);