我正在尝试使用SharedPreferences方法保存一个Arraylist。我得到的问题是我的索引超出范围,但我不明白为什么:
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
data = getSharedPreferences(filename,0);
SharedPreferences.Editor editor0 = data.edit();
editor0.putInt("shareddifficulty", difficulty);
editor0.putInt("sharedmoves", moves);
editor0.putInt("sharedpicture",resource);
editor0.putInt("ID_size", ID.size());
for(int i=0;i<ID.size();i++){
editor0.remove("ID_"+i);
editor0.putInt("ID_" + i, ID.get(i));
}
editor0.commit();
}
public void onResume(){
super.onResume();
data = getSharedPreferences(filename,0);
difficulty = data.getInt("shareddifficulty", 0);
moves = data.getInt("sharedmoves",0);
int listsize = data.getInt("ID_size", 0);
resource = data.getInt("sharedpicture", 0);
for(int i=0; i < listsize;i++){
ID.set(i,data.getInt("ID_" + i,0));
}
for(int i=0;i<listsize;i++){
crops.set(ID.get(i),cropsshuffle.get((ID.get(i))));
}
}
croples和ID列表大小相同。因为了解arraylists的样子非常重要,我将展示庄稼的代码和ID的代码:
作物:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
//Cropping the images and making a ID list that coincide with the cropped images.
divideImages(bmp);
带
private void divideImages(Bitmap bmp) {
int width = bmp.getWidth();
float w_ratio = (float) w/width;
int scaledwidth = (int) (bmp.getWidth()*w_ratio);
int scaledheight = (int) (bmp.getHeight()*w_ratio);
Bitmap scaledbmp = Bitmap.createScaledBitmap(bmp,scaledwidth,scaledheight,true);
switch(difficulty){
case 0:
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(i==2 && j==2){
Bitmap cropimage = Bitmap.createBitmap(scaledwidth/3,scaledheight/3,Bitmap.Config.ARGB_8888);
crops.add(cropimage);
cropimage = null;
}
else{
int startx = (int) ((scaledwidth*j)/3);
int starty = (int) ((scaledheight*i)/3);
Bitmap croppimage = Bitmap.createBitmap(scaledbmp,startx,starty,scaledwidth/3,scaledheight/3);
crops.add(croppimage);
croppimage = null;
}
}
grd.setNumColumns(3);
}
break;
case 1:
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i==3 && j==3){
Bitmap croppimage = Bitmap.createBitmap(scaledwidth/4,scaledheight/4,Bitmap.Config.ARGB_8888);
crops.add(croppimage);
croppimage = null;
}
else{
int startx = (int) ((scaledwidth*j)/4);
int starty = (int) ((scaledheight*i)/4);
Bitmap croppimage = Bitmap.createBitmap(scaledbmp,startx,starty,scaledwidth/4,scaledheight/4);
crops.add(croppimage);
croppimage = null;
}
}
grd.setNumColumns(4);
}
break;
case 2:
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(i==4 && j==4){
Bitmap croppimage = Bitmap.createBitmap(scaledwidth/5,scaledheight/5,Bitmap.Config.ARGB_8888);
crops.add(croppimage);
croppimage = null;
}
else{
int startx = (int) ((scaledwidth*j)/5);
int starty = (int) ((scaledheight*i)/5);
Bitmap croppimage = Bitmap.createBitmap(scaledbmp,startx,starty,scaledwidth/5,scaledheight/5);
crops.add(croppimage);
croppimage = null;
}
}
grd.setNumColumns(5);
}
break;
}
}
和ID列表:
private void getID(){
for(int i=0;i<crops.size();i++){
ID.add(i);
}
}
在我的gridview中,我有一个onitemclicklistener,如果我们交换作物和ID的图块,它会改变位置。
答案 0 :(得分:1)
我创建了一个库来缓解这种任务,只需包含我的lib:
compile 'com.cesarferreira.quickutils:library:2.2.1'
初始化:
QuickUtils.init(context);
然后您可以在任何地方轻松保存/阅读/删除:
QuickUtils.prefs.save(key, value);
QuickUtils.prefs.getInt(key, defaultValue);
QuickUtils.prefs.remove(key);
所有文档here
答案 1 :(得分:-2)
从上面的代码中我无法告诉你错误的位置。
但是当您尝试访问IndexOutofBoundsException
中不属于您的数组内的项目时,会发生Array
。
示例:强>
String[] myArray = new String[10];
myArray[10] = "test";
这会解析为IndexOutofBoundsException
,因为您的数组仅来自 0-9 。
基于上述错误示例,尝试找出代码中出现的问题。否则,请发布更具体的代码,因为正如您所看到的,您的错误围绕着Array
。