0
我在開發Android項目時遇到問題。 我想在一個文件中保存一個名爲arrayVersion的名爲'Version'的對象,以便在我的MainActivity開始時加載它。 與我發送的對象相比,返回的對象的字段沒有相同的值。發送加載arrayList Android
這裏是save()方法
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();
}
}
}
我在logcat的把我的版本水平的代碼。
這裏是我的加載方法的代碼
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()返回正確的尺寸和良好的元素(以不同的名稱版本),但現場級總是在0.
這裏是我的班級版本的代碼。
公共類版本實現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) {
}
}
這裏是我的課的Alpha,它擴展版
公共類阿爾法擴展版本{
/**
*
*/
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();}
}
}
的代碼
我希望這足以讓你幫助我,如果有的話是什麼你不明白的,不要猶豫,問我。
注:這是我第一次發帖,我真的開始在Android的編程
編輯:我的logcat是如下因素:
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
太多的解釋和代碼塊。請澄清你的問題。 – gokhanakkurt 2014-10-20 20:28:15
我想知道爲什麼在我的ArrayList中讀取的值與我寫入的值不同(方法保存並加載) – 2014-10-20 20:29:38
可以在保存之前,保存之後,加載之前和加載之後記錄值?我懷疑這個問題與你如何儲存或你如何讀取負載有關。 – erad 2014-10-20 20:45:09