我正在尝试使用两种SharedPreferences,第一种是有效的,而第二种则不是
这是我的Java代码
public class JogoActivity extends AppCompatActivity implements View.OnClickListener {
//OTHERS VARIABLES
public TextView txtViewResult,txtCoins, txtTentativas;
private static final String PREF_NAME = "JogoActivityPreferences";
int resulFinalCache,coinsFinalCache;
int count1, count2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
//methods...
//MY PROBLEMS START HERE
txtCoins = (TextView) findViewById(R.id.txtCoins);
txtViewResult = (TextView) findViewById(R.id.textViewResultado);
SharedPreferences sp1 = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
count1 = sp1.getInt("count1", 0);
txtViewResult.setText(String.valueOf(formatter.format(count1).toString()));//THIS IS WORKING
SharedPreferences sp2 = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
count2 = sp2.getInt("count2", 0);
txtCoins.setText(String.valueOf(formatter.format(count2).toString()));////THIS ISN'T
}
//Creating methods...
@Override
protected void onStop(){
super.onStop();
SharedPreferences sp1 = getSharedPreferences(PREF_NAME, MODE_PRIVATE);//THIS IS WORKING
count1 = sp1.getInt("count1", 0);
SharedPreferences.Editor editor = sp1.edit();
editor.putInt("count1", resulFinalCache);
editor.commit();
SharedPreferences sp2 = getSharedPreferences(PREF_NAME, MODE_PRIVATE);//THIS ISN'T
count2 = sp2.getInt("count2", 0);
editor = sp1.edit();
editor.putInt("count2", coinsFinalCache);
editor.commit();
}
@Override
public void onDestroy() {
super.onDestroy();
SharedPreferences sp1= getSharedPreferences(PREF_NAME, MODE_PRIVATE);//THIS IS WORKING
SharedPreferences.Editor editor = sp1.edit();
editor.putInt("count1", resulFinalCache);
editor.commit();
SharedPreferences sp2= getSharedPreferences(PREF_NAME, MODE_PRIVATE);//THIS ISN'T
editor = sp2.edit();
editor.putInt("count2", coinsFinalCache);
editor.commit();
}}
每次我启动应用程序,我使用,我关闭应用程序,并再次打开,TextView txtViewResult运行良好,但txtCoins不是
我试着只使用getPreferences而不是getSharedPreferences,但它不是工作
我尝试创建其他编辑器,但它不是太工作
我做错什么了吗?
非常感谢!
发布于 2016-03-10 19:37:13
不需要调用getsharedpreferences两次。
一次就好了,应该会很好的。
你的终点站应该是
SharedPreferences sp1 = getSharedPreferences(PREF_NAME, MODE_PRIVATE);//THIS IS WORKING
count1 = sp1.getInt("count1", 0);
SharedPreferences.Editor editor = sp1.edit();
editor.putInt("count1", resulFinalCache);
count2 = sp1.getInt("count2", 0);
editor.putInt("count2", coinsFinalCache);
editor.commit();发布于 2016-03-10 19:36:25
嗯。你的解决方案是
SharedPreferences sp1_2 = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
count1 = sp1_2.getInt("count1", 0);
SharedPreferences.Editor editor = sp1_2.edit();
editor.putInt("count1", resulFinalCache);
editor.commit();
count2 = sp1_2.getInt("count2", 0);
editor = sp1_2.edit();
editor.putInt("count2", coinsFinalCache);
editor.commit();
//now both will work实际上绑定方法
发布于 2016-03-10 19:36:33
一般来说,我可以建议你简单地使用
为节省:
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("name", 1).apply();阅读:
PreferenceManager.getDefaultSharedPreferences(this).getInt("name", 0);享受吧!
https://stackoverflow.com/questions/35925528
复制相似问题