2.3.4 中序列化 DecimalFormatSymbols 时出现问题

发布于 2024-11-05 17:59:11 字数 9793 浏览 5 评论 0原文

我有一些代码,我正在序列化包含 DecimalFormat 数字的类之一。这一直有效到 2.3.4。我该如何解决?

ERROR/AndroidRuntime(684): java.lang.IllegalArgumentException: no char field 'exponential'

这是我正在序列化的类:

public class Quote implements Serializable  {

    private static final long serialVersionUID = 1L;

    private String symbol;
    private String name;
    private String change;
    private String percentChange;
    private String open;
    private String daysHigh;
    private String daysLow;
    private String dividendYield;
    private String volume;
    private String averageDailyVolume;
    private String peRatio;
    private String marketCapitalization;
    private String yearHigh;
    private String yearLow;
    private String lastTradePriceOnly;
    private DecimalFormat df = new DecimalFormat("#,###,###,###,###,##0.00");
    private DecimalFormat vf = new DecimalFormat("#,###,###,###,###,##0");

    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getChange() {
        return change;
    }
    public void setChange(String change) {
        if(change.equals("null")){
            this.change = "N/A";
        }
        else{   
            float floatedChange = Float.valueOf(change);
            this.change = (df.format(floatedChange));
        }
    }
    public String getPercentChange() {
        return percentChange;
    }
    public void setPercentChange(String percentChange) {
        if(percentChange.equals("null"))
            percentChange = "N/A";
        else
            this.percentChange = percentChange;
    }
    public String getOpen() {
        return open;
    }
    public void setOpen(String open) {
        if(open.equals("null"))
            this.open = "N/A";
        else
            this.open = open;
    }
    public String getDaysHigh() {
        return daysHigh;
    }
    public void setDaysHigh(String daysHigh) {
        if(daysHigh.equals("null"))
            this.daysHigh = "N/A";
        else{
            float floatedDaysHigh = Float.valueOf(daysHigh);
            this.daysHigh = (df.format(floatedDaysHigh));
        }
    }
    public String getDaysLow() {
        return daysLow;
    }
    public void setDaysLow(String daysLow) {
        if(daysLow.equals("null"))
            this.daysLow = "N/A";
        else{
            float floatedDaysLow = Float.valueOf(daysLow);
            this.daysLow = (df.format(floatedDaysLow));
        }
    }
    public String getVolume() {
        return volume;
    }
    public void setVolume(String volume) {
        if(volume.equals("null")){
            this.volume = "N/A";
        }
        else{
            float floatedVolume = Float.valueOf(volume);
            this.volume = (vf.format(floatedVolume));
        }
    }
    public String getDividendYield() {
        return dividendYield;
    }
    public void setDividendYield(String dividendYield) {
        if(dividendYield.equals("null"))
            this.dividendYield = "N/A";
        else
            this.dividendYield = dividendYield;
    }
    public String getAverageDailyVolume() {
        return averageDailyVolume;
    }
    public void setAverageDailyVolume(String averageDailyVolume) {
        if(averageDailyVolume.equals("null")){
            this.averageDailyVolume = "N/A";
        }
        else{
            float floatedAverageDailyVolume = Float.valueOf(averageDailyVolume);
            this.averageDailyVolume = (vf.format(floatedAverageDailyVolume));
        }
    }
    public String getPeRatio() {
        return peRatio;
    }
    public void setPeRatio(String peRatio) {
        if(peRatio.equals("null"))
            this.peRatio = "N/A";
            else
        this.peRatio = peRatio;
    }
    public String getMarketCapitalization() {
        return marketCapitalization;
    }
    public void setMarketCapitalization(String marketCapitalization) {
        if(marketCapitalization.equals("null"))
            this.marketCapitalization = "N/A";
        else
            this.marketCapitalization = marketCapitalization;
    }
    public String getYearHigh() {
        return yearHigh;
    }
    public void setYearHigh(String yearHigh) {
        if(yearHigh.equals("null"))
            this.yearHigh = "N/A";
        else
            this.yearHigh = yearHigh;
    }
    public String getYearLow() {
        return yearLow;
    }
    public void setYearLow(String yearLow) {
        if(yearLow.equals("null"))
            this.yearLow = "N/A";
        else
            this.yearLow = yearLow;
    }

    public String getLastTradePriceOnly() {
        return lastTradePriceOnly;
    }

    public void setLastTradePriceOnly(String lastTradePriceOnly) {
        if(lastTradePriceOnly.equals("null")){
            this.lastTradePriceOnly = "N/A";
        }
        else{
            float floatedLastTradePriceOnly = Float.valueOf(lastTradePriceOnly);
            this.lastTradePriceOnly = (df.format(floatedLastTradePriceOnly));
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((change == null) ? 0 : change.hashCode());
        result = prime * result
                + ((daysHigh == null) ? 0 : daysHigh.hashCode());
        result = prime * result + ((daysLow == null) ? 0 : daysLow.hashCode());
        result = prime
                * result
                + ((lastTradePriceOnly == null) ? 0 : lastTradePriceOnly
                        .hashCode());
        result = prime
                * result
                + ((marketCapitalization == null) ? 0 : marketCapitalization
                        .hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((open == null) ? 0 : open.hashCode());
        result = prime * result + ((peRatio == null) ? 0 : peRatio.hashCode());
        result = prime * result
                + ((percentChange == null) ? 0 : percentChange.hashCode());
        result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
        result = prime * result + ((volume == null) ? 0 : volume.hashCode());
        result = prime * result
                + ((yearHigh == null) ? 0 : yearHigh.hashCode());
        result = prime * result + ((yearLow == null) ? 0 : yearLow.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Quote other = (Quote) obj;
        if (change == null) {
            if (other.change != null)
                return false;
        } else if (!change.equals(other.change))
            return false;
        if (daysHigh == null) {
            if (other.daysHigh != null)
                return false;
        } else if (!daysHigh.equals(other.daysHigh))
            return false;
        if (daysLow == null) {
            if (other.daysLow != null)
                return false;
        } else if (!daysLow.equals(other.daysLow))
            return false;
        if (lastTradePriceOnly == null) {
            if (other.lastTradePriceOnly != null)
                return false;
        } else if (!lastTradePriceOnly.equals(other.lastTradePriceOnly))
            return false;
        if (marketCapitalization == null) {
            if (other.marketCapitalization != null)
                return false;
        } else if (!marketCapitalization.equals(other.marketCapitalization))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (open == null) {
            if (other.open != null)
                return false;
        } else if (!open.equals(other.open))
            return false;
        if (peRatio == null) {
            if (other.peRatio != null)
                return false;
        } else if (!peRatio.equals(other.peRatio))
            return false;
        if (percentChange == null) {
            if (other.percentChange != null)
                return false;
        } else if (!percentChange.equals(other.percentChange))
            return false;
        if (symbol == null) {
            if (other.symbol != null)
                return false;
        } else if (!symbol.equals(other.symbol))
            return false;
        if (volume == null) {
            if (other.volume != null)
                return false;
        } else if (!volume.equals(other.volume))
            return false;
        if (yearHigh == null) {
            if (other.yearHigh != null)
                return false;
        } else if (!yearHigh.equals(other.yearHigh))
            return false;
        if (yearLow == null) {
            if (other.yearLow != null)
                return false;
        } else if (!yearLow.equals(other.yearLow))
            return false;
        return true;
    }
}

这是我的序列化操作(注意引号是一个数组列表):

private void serializeQuotes() {
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes); //this is an ArrayList<Quote>
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I have some code where I am serializing one of my classes that contains a DecimalFormat number. This worked up until 2.3.4. How can I resolve?

ERROR/AndroidRuntime(684): java.lang.IllegalArgumentException: no char field 'exponential'

Here is the class that I am serializing:

public class Quote implements Serializable  {

    private static final long serialVersionUID = 1L;

    private String symbol;
    private String name;
    private String change;
    private String percentChange;
    private String open;
    private String daysHigh;
    private String daysLow;
    private String dividendYield;
    private String volume;
    private String averageDailyVolume;
    private String peRatio;
    private String marketCapitalization;
    private String yearHigh;
    private String yearLow;
    private String lastTradePriceOnly;
    private DecimalFormat df = new DecimalFormat("#,###,###,###,###,##0.00");
    private DecimalFormat vf = new DecimalFormat("#,###,###,###,###,##0");

    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getChange() {
        return change;
    }
    public void setChange(String change) {
        if(change.equals("null")){
            this.change = "N/A";
        }
        else{   
            float floatedChange = Float.valueOf(change);
            this.change = (df.format(floatedChange));
        }
    }
    public String getPercentChange() {
        return percentChange;
    }
    public void setPercentChange(String percentChange) {
        if(percentChange.equals("null"))
            percentChange = "N/A";
        else
            this.percentChange = percentChange;
    }
    public String getOpen() {
        return open;
    }
    public void setOpen(String open) {
        if(open.equals("null"))
            this.open = "N/A";
        else
            this.open = open;
    }
    public String getDaysHigh() {
        return daysHigh;
    }
    public void setDaysHigh(String daysHigh) {
        if(daysHigh.equals("null"))
            this.daysHigh = "N/A";
        else{
            float floatedDaysHigh = Float.valueOf(daysHigh);
            this.daysHigh = (df.format(floatedDaysHigh));
        }
    }
    public String getDaysLow() {
        return daysLow;
    }
    public void setDaysLow(String daysLow) {
        if(daysLow.equals("null"))
            this.daysLow = "N/A";
        else{
            float floatedDaysLow = Float.valueOf(daysLow);
            this.daysLow = (df.format(floatedDaysLow));
        }
    }
    public String getVolume() {
        return volume;
    }
    public void setVolume(String volume) {
        if(volume.equals("null")){
            this.volume = "N/A";
        }
        else{
            float floatedVolume = Float.valueOf(volume);
            this.volume = (vf.format(floatedVolume));
        }
    }
    public String getDividendYield() {
        return dividendYield;
    }
    public void setDividendYield(String dividendYield) {
        if(dividendYield.equals("null"))
            this.dividendYield = "N/A";
        else
            this.dividendYield = dividendYield;
    }
    public String getAverageDailyVolume() {
        return averageDailyVolume;
    }
    public void setAverageDailyVolume(String averageDailyVolume) {
        if(averageDailyVolume.equals("null")){
            this.averageDailyVolume = "N/A";
        }
        else{
            float floatedAverageDailyVolume = Float.valueOf(averageDailyVolume);
            this.averageDailyVolume = (vf.format(floatedAverageDailyVolume));
        }
    }
    public String getPeRatio() {
        return peRatio;
    }
    public void setPeRatio(String peRatio) {
        if(peRatio.equals("null"))
            this.peRatio = "N/A";
            else
        this.peRatio = peRatio;
    }
    public String getMarketCapitalization() {
        return marketCapitalization;
    }
    public void setMarketCapitalization(String marketCapitalization) {
        if(marketCapitalization.equals("null"))
            this.marketCapitalization = "N/A";
        else
            this.marketCapitalization = marketCapitalization;
    }
    public String getYearHigh() {
        return yearHigh;
    }
    public void setYearHigh(String yearHigh) {
        if(yearHigh.equals("null"))
            this.yearHigh = "N/A";
        else
            this.yearHigh = yearHigh;
    }
    public String getYearLow() {
        return yearLow;
    }
    public void setYearLow(String yearLow) {
        if(yearLow.equals("null"))
            this.yearLow = "N/A";
        else
            this.yearLow = yearLow;
    }

    public String getLastTradePriceOnly() {
        return lastTradePriceOnly;
    }

    public void setLastTradePriceOnly(String lastTradePriceOnly) {
        if(lastTradePriceOnly.equals("null")){
            this.lastTradePriceOnly = "N/A";
        }
        else{
            float floatedLastTradePriceOnly = Float.valueOf(lastTradePriceOnly);
            this.lastTradePriceOnly = (df.format(floatedLastTradePriceOnly));
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((change == null) ? 0 : change.hashCode());
        result = prime * result
                + ((daysHigh == null) ? 0 : daysHigh.hashCode());
        result = prime * result + ((daysLow == null) ? 0 : daysLow.hashCode());
        result = prime
                * result
                + ((lastTradePriceOnly == null) ? 0 : lastTradePriceOnly
                        .hashCode());
        result = prime
                * result
                + ((marketCapitalization == null) ? 0 : marketCapitalization
                        .hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((open == null) ? 0 : open.hashCode());
        result = prime * result + ((peRatio == null) ? 0 : peRatio.hashCode());
        result = prime * result
                + ((percentChange == null) ? 0 : percentChange.hashCode());
        result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
        result = prime * result + ((volume == null) ? 0 : volume.hashCode());
        result = prime * result
                + ((yearHigh == null) ? 0 : yearHigh.hashCode());
        result = prime * result + ((yearLow == null) ? 0 : yearLow.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Quote other = (Quote) obj;
        if (change == null) {
            if (other.change != null)
                return false;
        } else if (!change.equals(other.change))
            return false;
        if (daysHigh == null) {
            if (other.daysHigh != null)
                return false;
        } else if (!daysHigh.equals(other.daysHigh))
            return false;
        if (daysLow == null) {
            if (other.daysLow != null)
                return false;
        } else if (!daysLow.equals(other.daysLow))
            return false;
        if (lastTradePriceOnly == null) {
            if (other.lastTradePriceOnly != null)
                return false;
        } else if (!lastTradePriceOnly.equals(other.lastTradePriceOnly))
            return false;
        if (marketCapitalization == null) {
            if (other.marketCapitalization != null)
                return false;
        } else if (!marketCapitalization.equals(other.marketCapitalization))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (open == null) {
            if (other.open != null)
                return false;
        } else if (!open.equals(other.open))
            return false;
        if (peRatio == null) {
            if (other.peRatio != null)
                return false;
        } else if (!peRatio.equals(other.peRatio))
            return false;
        if (percentChange == null) {
            if (other.percentChange != null)
                return false;
        } else if (!percentChange.equals(other.percentChange))
            return false;
        if (symbol == null) {
            if (other.symbol != null)
                return false;
        } else if (!symbol.equals(other.symbol))
            return false;
        if (volume == null) {
            if (other.volume != null)
                return false;
        } else if (!volume.equals(other.volume))
            return false;
        if (yearHigh == null) {
            if (other.yearHigh != null)
                return false;
        } else if (!yearHigh.equals(other.yearHigh))
            return false;
        if (yearLow == null) {
            if (other.yearLow != null)
                return false;
        } else if (!yearLow.equals(other.yearLow))
            return false;
        return true;
    }
}

And here is my serialize operation (note quotes is an array list):

private void serializeQuotes() {
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes); //this is an ArrayList<Quote>
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-11-12 17:59:11

为什么要序列化 ​​DecimalFormat ?将实例 DecimalFormat 字段替换为 static 字段。如果需要考虑并发性,请使用static ThreadLocal

public class Quote implements Serializable  {

    private static final long serialVersionUID = 1L;

    private String symbol;
    private String name;
    private String change;
    private String percentChange;
    private String open;
    private String daysHigh;
    private String daysLow;
    private String dividendYield;
    private String volume;
    private String averageDailyVolume;
    private String peRatio;
    private String marketCapitalization;
    private String yearHigh;
    private String yearLow;
    private String lastTradePriceOnly;


    private static final ThreadLocal<DecimalFormat> df = new ThreadLocal<DecimalFormat> {
        @Override
        protected DecimalFormat initialValue() {
            return new DecimalFormat("#,###,###,###,###,##0.00");
        }
    }

    private static final ThreadLocal<DecimalFormat> vf = new ThreadLocal<DecimalFormat> {
        @Override
        protected DecimalFormat initialValue() {
            return new DDecimalFormat("#,###,###,###,###,##0");
        }
    }

    // snip...

}

http://download.oracle.com/javase/6 /docs/api/java/lang/ThreadLocal.html

Why are you serializing a DecimalFormat at all? Replace the instance DecimalFormat fields with static ones. If concurrency is a concern, use static ThreadLocal<DecimalFormat>s.

public class Quote implements Serializable  {

    private static final long serialVersionUID = 1L;

    private String symbol;
    private String name;
    private String change;
    private String percentChange;
    private String open;
    private String daysHigh;
    private String daysLow;
    private String dividendYield;
    private String volume;
    private String averageDailyVolume;
    private String peRatio;
    private String marketCapitalization;
    private String yearHigh;
    private String yearLow;
    private String lastTradePriceOnly;


    private static final ThreadLocal<DecimalFormat> df = new ThreadLocal<DecimalFormat> {
        @Override
        protected DecimalFormat initialValue() {
            return new DecimalFormat("#,###,###,###,###,##0.00");
        }
    }

    private static final ThreadLocal<DecimalFormat> vf = new ThreadLocal<DecimalFormat> {
        @Override
        protected DecimalFormat initialValue() {
            return new DDecimalFormat("#,###,###,###,###,##0");
        }
    }

    // snip...

}

http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文