【问题标题】:SharedPreferences keep getting default valueSharedPreferences 不断获得默认值
【发布时间】:2012-10-31 12:28:09
【问题描述】:

我一直在获取默认值,或者我的 UI 将显示 null,或者如果我使用整数,它也会显示该默认值,这里它是字符串形式,请帮助

//putting the information in shared preferences
TextView 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);

【问题讨论】:

  • 尝试查看xml,看看commit是否成功。
  • 你没有在编辑后调用提交..

标签: android sharedpreferences


【解决方案1】:

我已为您的代码添加了一些注释,请检查:

//putting the information in shared preferences
TextView 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);

【讨论】:

  • 这个答案也解决了我的类似问题,我忘记输入 editor.commit();添加后它起作用了! :) 谢谢
【解决方案2】:

这一行

editor.putString(newUserScore1, null);

应该是

editor.putString("NewuserScore1",newUserScore1);

并且不要忘记使用editor.commit();提交您的更改

【讨论】:

  • capitol N 哈哈,我是有史以来最糟糕的打字员
  • 很高兴我能帮上忙。祝你好运!
【解决方案3】:

当您使用 SharedPreference 时,永远不要忘记致电 commit() 以保存您的更改。

    SharedPreferences.Editor editor = peepsScores2.edit();
    editor.putString("NewuserScore1", newUserScore1);
    editor.commit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多