在共享偏好中存储前5个分数失败

时间:2015-11-16 05:06:44

标签: java android

我试图将前5个高分存储在sharedpreference而不是sqlite数据库中。所以我环顾四周,从堆栈帖子中找到了两个方法(saveArray和loadArray)(Is it possible to add an array or object to SharedPreferences on Android

无论如何,当我尝试向数组添加值时,我收到arrayoutofbounds错误。 java.lang.ArrayIndexOutOfBoundsException: length=0;index=0

因为用户不会从数组中的值开始,我试图确定数组是否在 i 的位置有值,如果不是我的话试图插入在该位置传递给数组的分数。但它不会工作,我不知道为什么?

基本上如果highscores[i] != null那么我会去查看是否highscores[i] < intScore,如果highscores is < intScore那么我将用构造函数中传递的分数替换它。

String[] highscores = new String[5];

public void editHighscores(String score){
    int intScore = Integer.parseInt(score);


    highscores = loadArray("highscores",this.getApplicationContext());

    // Checks to see if index is empty
    for(int i=0;i<5; i++){
        if(highscores[i] == null){
            highscores[i] = score;
        } else{
            if(Integer.parseInt(highscores[i]) < intScore){
                highscores[i] = score;
            }
        }

        Log.d("index - " + i, "Value = " + highscores[i]);
    }
    // Save the array
    saveArray(highscores, "highscores", this.getApplicationContext());




}
public boolean saveArray(String[] array, String arrayName, Context mContext) {   
    SharedPreferences prefs = mContext.getSharedPreferences("highscoresPref", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  
    for(int i=0;i<array.length;i++)  
        editor.putString(arrayName + "_" + i, array[i]);  
    return editor.commit();  
} 

public String[] loadArray(String arrayName, Context mContext) {  
    SharedPreferences prefs = mContext.getSharedPreferences("highscoresPref", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    String array[] = new String[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getString(arrayName + "_" + i, null);  
    return array;  
}  

3 个答案:

答案 0 :(得分:0)

试试这段代码

String[] highscores = new String[5];

  public void editHighscores(String score){
int intScore = Integer.parseInt(score);


highscores = loadArray("highscores",this.getApplicationContext());

 if(highscores!=null && highscroes.length>0)
     {
    // Checks to see if index is empty
      for(int i=0;i<5; i++){
       if(highscores[i] == null){
           highscores[i] = score;
       } else{
        if(Integer.parseInt(highscores[i]) < intScore){
            highscores[i] = score;
        }
    }

    Log.d("index - " + i, "Value = " + highscores[i]);
}
// Save the array
saveArray(highscores, "highscores", this.getApplicationContext());


   }

  }

答案 1 :(得分:0)

loadArray()方法中,size默认为0.在第一次运行时,不会有任何已保存的首选项,因此size为0,这就是你'用来标注数组。

由于您在editHighscores()方法中检查空值,因此无需跟踪大小。你已经确定了数组应该有多大:5。

答案 2 :(得分:0)

最初int size = prefs.getInt(arrayName + "_size", 0);将返回大小0,因为未存储数组大小。

因此,loadArray方法会return empty Array ..

editHighscores方法中,您尝试在空数组上插入得分,这将产生ArrayIndexOutOfBoundsException

因此,要解决替换int size = prefs.getInt(arrayName + "_size", 0);int size = prefs.getInt(arrayName + "_size", 5);