Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SharedReferences不断获取默认值_Android_Sharedpreferences - Fatal编程技术网

Android SharedReferences不断获取默认值

Android SharedReferences不断获取默认值,android,sharedpreferences,Android,Sharedpreferences,我一直在获取默认值,或者我的UI将显示null,或者如果我使用整数,它也会显示默认值,这里是字符串形式的plz帮助 //putting the information in shared preferences TextView pScore1=(TextView)findViewById(R.id.pScore1f); SharedPreferences peepsScores2= PreferenceManager.getDefaultSharedPreferences(GamePlay

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

//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);

无论何时使用SharedReference,都不要忘记调用
commit()
保存更改

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

editor.putString(newUserScore1,null)

应该是

editor.putString("NewuserScore1",newUserScore1);

另外,不要忘记使用
editor.commit()提交更改

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

//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);

尝试查看xml并查看提交是否成功。编辑后您没有调用提交..提交是我在cop粘贴中错过的,当我将null更改为newUserScore1时,它在我的UIcapitol中仍然显示null。哈哈,我是有史以来最差的打字机。很高兴我能帮上忙。祝你好运这个答案也解决了我类似的问题,我忘记输入editor.commit();在加上它有效后!!:)谢谢