繁体   English   中英

SharedPreferences保存不正确-Android

[英]SharedPreferences not saving properly - android

我正在尝试使用sharedpreferences存储一个整数。 但是出了点问题,因此整数将不会被保存。 每当我停止我的应用程序时,整数都不会显示,它不存在并且没有得到保存。 这是我的onCreate()方法中的代码:

private int point;

//...

SharedPreferences sp = getSharedPreferences("prefs_file",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", point);
editor.apply();

SharedPreferences sp1 = getSharedPreferences("prefs_file",MODE_PRIVATE);
int highScoreSaved = sp1.getInt("your_int_key", 0);

尝试使用sp1.commit()。

看看开发者指南:

https://developer.android.com/guide/topics/data/data-storage.html#pref

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM