android–SharedPreferences继续获取默认值

android–SharedPreferences继续获取默认值,第1张

概述我保持获取默认值要么我的UI将显示为null,要么我使用整数它显示默认值以及它在字符串形式plz帮助//puttingtheinformationinsharedpreferencesTextViewpScore1=(TextView)findViewById(R.id.pScore1f);SharedPreferencespeepsScores2=PreferenceManager.getDefaultSh

我保持获取默认值要么我的UI将显示为null,要么我使用整数它显示默认值以及它在字符串形式plz帮助

//putting the information in shared preferencesTextVIEw pscore1=(TextVIEw)findVIEwByID(R.ID.pscore1f);SharedPreferences peepsscores2= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);SharedPreferences.Editor editor2 =peepsscores2.edit();String userscore11 = pscore1.getText().toString();  editor2.putString("userscore11",userscore11);  editor2.commit();  //getting it and editing it  SharedPreferences peepsscores2 = PreferenceManager.getDefaultSharedPreferences(this);    int u;    int one =1;    int newUsrscore1=1;    String userscore11 = peepsscores2.getString("userscore11",null);    u=Integer.parseInt(userscore11);        newUsrscore1 = u+one;        String newUserscore1  = Integer.toString(newUsrscore1);    SharedPreferences.Editor editor = peepsscores2.edit();    editor.putString(newUserscore1, NewUserscore1);      editor.commit();    //getting it and displaying it on the UI    SharedPreferences peepsscores2 = PreferenceManager.getDefaultSharedPreferences(this);    String userscore11 = peepsscores2.getString("Newuserscore1",null);  pscore1.setText(" "+userscore11);

解决方法:

我已经为您添加了一些评论代码,请检查:

//putting the information in shared preferencesTextVIEw pscore1=(TextVIEw)findVIEwByID(R.ID.pscore1f);SharedPreferences peepsscores2= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);SharedPreferences.Editor editor2 =peepsscores2.edit();String userscore11 = pscore1.getText().toString();  editor2.putString("userscore11",userscore11);  editor2.commit();  //getting it and editing it  SharedPreferences peepsscores2 = PreferenceManager.getDefaultSharedPreferences(this);    int u;    int one =1;    int newUsrscore1=1;    String userscore11 = peepsscores2.getString("userscore11",null);    u=Integer.parseInt(userscore11);        newUsrscore1 = u+one;        String newUserscore1  = Integer.toString(newUsrscore1);    SharedPreferences.Editor editor = peepsscores2.edit();     //@Praful: here newUserscore1 seems to be integer value and you are storing     //null here. I think it it should be     //`editor.putString("Newuserscore1", newUsrscore1);`    editor.putString(newUserscore1, null);     //@Praful: call commit here    editor.commit;    //getting it and displaying it on the UI    SharedPreferences peepsscores2 = PreferenceManager.getDefaultSharedPreferences(this);    String userscore11 = peepsscores2.getString("Newuserscore1",null);  pscore1.setText(" "+userscore11);
总结

以上是内存溢出为你收集整理的android – SharedPreferences继续获取默认值全部内容,希望文章能够帮你解决android – SharedPreferences继续获取默认值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存