【问题标题】:Issue loading arrayList Android问题加载arrayList Android
【发布时间】:2014-12-15 22:19:11
【问题描述】:

我正在开发的 Android 项目中遇到问题。 我想在一个文件中保存一个名为“版本”的对象的数组列表,以便在我的 MainActivity 开始时加载它。 与我发送的对象相比,返回的对象的字段值不同。

这里是 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。

这是我的班级版本的代码。

公共类版本实现可序列化{

/**
 * 
 */
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 类的代码,它扩展了 Version

公共类 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

【问题讨论】:

  • 解释和代码块太多。请澄清您的问题。
  • 我想知道为什么我在 ArrayList 中读取的值与我写入的值不同(方法保存和加载)
  • 您可以在保存前、保存后、加载前和加载后记录您的值吗?我怀疑问题在于您如何保存或如何读取负载。

标签: java android arraylist save serializable


【解决方案1】:

问题已解决: 我的班级版本中的静态字段没有写入我尝试保存和加载的数组中。

不要将静态字段放在可序列化的类中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多