Search code examples
javaandroidarraylistsaveserializable

Issue loading arrayList Android


I'm having a problem in an Android project i'm developping. I want to save an arrayList called arrayVersion of objects called 'Version' in a file in order to load it on start of my MainActivity. The fields of the object returned doen't have the same values compared to the object i send.

Here is the code of the save() method

public void save(){
if (arrayVersion.size()!=0){
            ObjectOutput out = null;
            try {
                Log.d("save", "save");
                out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+File.separator+"Version.log"));
                Log.d("save", ""+arrayVersion.get(0).getLevel()); //Writes 6 for instance
                out.writeObject(arrayVersion); 
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

I'm putting in the logCat the level of my version.

Here is the code of my loading method

private void load() {

//Initialisation of my arrayList
if(arrayVersion.size()==0){
    for (int j = 0; j <= nbVersions; j++) {
        MainActivity.arrayVersion.add(Version.getVersion(j));
    }
}  
    try {
    ObjectInputStream  is = new ObjectInputStream(new FileInputStream(new File(new File(getFilesDir(),"")+File.separator+"Version.log")));
    arrayVersion = (ArrayList<Version>) is.readObject();
    Log.d("load", "load"+arrayVersion.size()); //Writes the good size
    Log.d("load", ""+arrayVersion.get(0).getName());//writes the good name
    Log.d("load", ""+arrayVersion.get(0).getLevel());//writes 0 everytime

    is.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }


}

When i reload my application, the arrayVersion.size() returns the right size and the good elements (Versions with different names), but the field level is always at 0.

here is the code of my Class Version.

public class Version implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 8733931469883869946L;
protected static int level;
protected static double price;
protected static double ups;
protected String name;
protected int upsTag;
protected int priceTag;
protected int levelTag;
protected static int index;
protected double modifier;
protected double DEFAULT_UPS;


public Version() {

}

public int getIndex() {
    // 
    return index;
}

public double getUps() {
    // 
    return ups;
}

public double getPrice() {
    // 
    return price;
}

public void upgrade() {
    price*=1.10;
    level++;
    Log.d("upgrade","niveau" + level);  
}

public void calcUps() {
    // 

}

public int getLevel() {
    // 
    return level;
}

public int getUpsTag() {
    // 
    return upsTag;
}

public int getPriceTag() {
    // 
    return priceTag;
}

public int getLevelTag() {
    // 
    return levelTag;
}

public String getName() {
    // 
    return name;
}

public static Version getVersion(int index) {
    Version version = null;
    if(index == 0)
        version = Alpha.getAlpha();
    if (index == 1)
        version = Beta.getBeta();
    if (index == 2)
        version = ApplePie.getApplePie();
    if (index == 3)
        version = BananaBread.getBananaBread();
    if (index == 4)
        version = Cupcake.getCupcake();
    if (index == 5)
        version = Donut.getDonut();
    if (index == 6)
        version = Eclair.getEclair();


    return version;
}

public void upgrade(int index) {

}

}

And here is the code of my class Alpha, which extends Version

public class Alpha extends Version{

/**
 * 
 */
private static final long serialVersionUID = -4246008928649558154L;
private String name = "alpha";
private static Alpha alpha;

Alpha() {
    modifier = 1;
    level = 0;
    price=10;
    DEFAULT_UPS=0.1;
    ups=0;
    index = 0;
    upsTag = R.id.AlphaUps;
    priceTag = R.id.AlphaPrice;
    levelTag = R.id.AlphaLevel;
}

public static Alpha getAlpha() {
    if (alpha==null)
        alpha = new Alpha();
    return alpha;
}
public void upgrade()
{
price*=1.10;
level++;
Log.d("upgrade","niveau" + level);
if (level==10){UpgradesActivity.setTag(21);}
if (level==25){UpgradesActivity.setTag(22);}
if (level==50){UpgradesActivity.setTag(23);}
if (level==100){UpgradesActivity.setTag(24);}
if (level==150){UpgradesActivity.setTag(25);}
if (level==200){UpgradesActivity.setTag(26);}
if (level==250){UpgradesActivity.setTag(27);}

}


public int getIndex() {
    return  index;
}

public int getLevel() {
    return level;
}

public double getPrice() {
    return price;
}

public void calcUps() {
    ups = level*DEFAULT_UPS*modifier;
    Log.d("alpha", String.valueOf(ups));    
    }
public double getUps() {
    return ups; 
    }
public String getName()
{
    return name;
}

public void upgrade (int index){
    if (index ==21){modifier *= 2;calcUps();}
    if (index ==22){modifier *= 2;calcUps();}
    if (index ==23){modifier *= 2;calcUps();}
    if (index ==24){modifier *= 2;calcUps();}
    if (index ==25){modifier *= 2;calcUps();}
    if (index ==26){modifier *= 2;calcUps();}
    if (index ==27){modifier *= 2;calcUps();}


}
}

I hope this is enough for you to help me, and if there is anything you don't understand, do not hesitate to ask me.

Note : this is my first ever post and i'm really beginning in Android Programming

Edit : my LogCat is the folowing :

10-20 23:01:22.099: D/stack(23782): before load :alpha &0
10-20 23:01:22.099: D/stack(23782): before load :beta &0
10-20 23:01:22.099: D/stack(23782): before load :apple pie &0
10-20 23:01:22.099: D/stack(23782): before load :bananaBread &0
10-20 23:01:22.099: D/stack(23782): before load :cupcake &0
10-20 23:01:22.099: D/stack(23782): before load :donut &0
10-20 23:01:22.099: D/stack(23782): before load :eclair &0
10-20 23:01:22.149: D/stack(23782): after load :alpha &0
10-20 23:01:46.213: D/stack(23782): before save :alpha &12
10-20 23:01:46.293: D/stack(23782): after save : alpha &12
10-20 23:02:08.555: D/stack(23782): before save :alpha &12
10-20 23:02:08.595: D/stack(23782): after save : alpha &12
10-20 23:02:28.904: D/stack(23782): before save :alpha &12
10-20 23:02:28.904: D/stack(23782): before save :beta &7
10-20 23:02:28.954: D/stack(23782): after save : alpha &12
10-20 23:02:28.954: D/stack(23782): after save : beta &7

Solution

  • Problem solved : The static fields in my class Version were not writing in the array that i tried to save and load.

    Do not put a static field in a serializable class.