2014-10-20 3 views
0

Android 프로젝트에 문제가 있습니다. 개발 중입니다. MainActivity가 시작될 때 파일에 'Version'이라는 객체의 arrayVersion이라는 arrayList를 저장하려고합니다. 반환 된 객체의 필드는 내가 보낸 객체와 동일한 값을 갖지 않습니다.로드하는 중 arrayList Android

는 여기가 로그 캣에서 내 버전의 수준을두고있어 저장() 메소드

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(); 
      } 
     } 
} 

의 코드입니다. 여기

내 로딩 방식의 코드

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(); 
    } 


} 

난 내 응용 프로그램을 다시로드 할 때, arrayVersion.size()는 적당한 크기와 좋은 요소 (이름이 다른 버전),하지만 필드 레벨을 반환

여기 내 수업 버전 코드입니다.

공용 클래스 버전 직렬화 {

/** 
* 
*/ 
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) { 

} 

}

구현 그리고 여기 버전

알파 확장

공용 클래스 버전 {

/** 
* 
*/ 
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();} 


} 
} 
를 확장 내 수업 알파의 코드

나에게 도움이 되었으면 좋겠다. 당신이 이해하지 못하는 것은 무엇이든, 저에게 물어 보는 것을 망설이지 마십시오.

참고 :이 게시 사상 처음이고 난 정말

편집을 프로그래밍 안드로이드에 시작 해요 :

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 
+0

너무 많은 설명과 코드 블록. 귀하의 질문을 명확히하십시오. – gokhanakkurt

+0

왜 내가 값을 내가 ArrayList에 arren't 읽기 (메서드 저장 및로드) –

+0

저장하기 전에 당신의 값을 저장할 수 있는지 알고 싶습니다 저장하기 전에,로드하기 전에,로드 후로드? 문제가 저장하는 방법이나로드를 읽는 방법에 문제가 있다고 생각됩니다. – erad

답변

0

문제 해결 : 내 로그 캣은 folowing이 내 수업 시간에 정적 필드 버전은 내가 저장하고로드하려고 시도하는 배열에 쓰지 않았습니다.

직렬화 가능 클래스에 정적 필드를 두지 마십시오.