问题2.3.4序列化的DecimalFormatSymbols [英] Problem serializing DecimalFormatSymbols in 2.3.4

查看:153
本文介绍了问题2.3.4序列化的DecimalFormatSymbols的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,其中我序列化包含一个DecimalFormat一些我的课之一。这个工作,直到2.3.4。如何解决?

  ERROR / AndroidRuntime(684):java.lang.IllegalArgumentException异常:无炭场'指数'

下面是我的序列化类:

 公共类报价实现Serializable {    私有静态最后的serialVersionUID长1L =;    私人字符串符号;
    私人字符串名称;
    私人字符串的变化;
    私人字符串percentChange;
    私人字符串打开;
    私人字符串daysHigh;
    私人字符串daysLow;
    私人字符串dividendYield;
    私人字符串量;
    私人字符串averageDailyVolume;
    私人字符串操作;
    私人字符串marketCapitalization;
    私人字符串yearHigh;
    私人字符串yearLow;
    私人字符串lastTradePriceOnly;
    私人DecimalFormat的DF =新的DecimalFormat(#,###,###,###,###,## 0.00);
    私人DecimalFormat的VF =新的DecimalFormat(#,###,###,###,###,## 0);    公共字符串getSymbol(){
        返回符号;
    }
    公共无效setSymbol(字符串符号){
        this.symbol =符号;
    }
    公共字符串的getName(){
        返回名称;
    }
    公共无效setname可以(字符串名称){
        this.name =名称;
    }
    公共字符串getChange(){
        返回的变化;
    }
    公共无效setChange(字符串变化){
        如果(change.equals(空)){
            this.change =N / A;
        }
        其他{
            浮floatedChange = Float.valueOf(变化);
            this.change =(df.format(floatedChange));
        }
    }
    公共字符串getPercentChange(){
        返回percentChange;
    }
    公共无效setPercentChange(字符串percentChange){
        如果(percentChange.equals(空))
            percentChange =N / A;
        其他
            this.percentChange = percentChange;
    }
    公共字符串getOpen(){
        返回打开;
    }
    公共无效就是setOpen(串开路){
        如果(open.equals(空))
            this.open =N / A;
        其他
            this.open =开;
    }
    公共字符串getDaysHigh(){
        返回daysHigh;
    }
    公共无效setDaysHigh(字符串daysHigh){
        如果(daysHigh.equals(空))
            this.daysHigh =N / A;
        其他{
            浮floatedDaysHigh = Float.valueOf(daysHigh);
            this.daysHigh =(df.format(floatedDaysHigh));
        }
    }
    公共字符串getDaysLow(){
        返回daysLow;
    }
    公共无效setDaysLow(字符串daysLow){
        如果(daysLow.equals(空))
            this.daysLow =N / A;
        其他{
            浮floatedDaysLow = Float.valueOf(daysLow);
            this.daysLow =(df.format(floatedDaysLow));
        }
    }
    公共字符串getVolume(){
        回流量;
    }
    公共无效setVolume(字符串音量){
        如果(volume.equals(空)){
            this.volume =N / A;
        }
        其他{
            浮floatedVolume = Float.valueOf(体积);
            this.volume =(vf.format(floatedVolume));
        }
    }
    公共字符串getDividendYield(){
        返回dividendYield;
    }
    公共无效setDividendYield(字符串dividendYield){
        如果(dividendYield.equals(空))
            this.dividendYield =N / A;
        其他
            this.dividendYield = dividendYield;
    }
    公共字符串getAverageDailyVolume(){
        返回averageDailyVolume;
    }
    公共无效setAverageDailyVolume(字符串averageDailyVolume){
        如果(averageDailyVolume.equals(空)){
            this.averageDailyVolume =N / A;
        }
        其他{
            浮floatedAverageDailyVolume = Float.valueOf(averageDailyVolume);
            this.averageDailyVolume =(vf.format(floatedAverageDailyVolume));
        }
    }
    公共字符串getPeRatio(){
        返回操作;
    }
    公共无效setPeRatio(字符串操作){
        如果(peRatio.equals(空))
            this.peRatio =N / A;
            其他
        this.peRatio =操作;
    }
    公共字符串getMarketCapitalization(){
        返回marketCapitalization;
    }
    公共无效setMarketCapitalization(字符串marketCapitalization){
        如果(marketCapitalization.equals(空))
            this.marketCapitalization =N / A;
        其他
            this.marketCapitalization = marketCapitalization;
    }
    公共字符串getYearHigh(){
        返回yearHigh;
    }
    公共无效setYearHigh(字符串yearHigh){
        如果(yearHigh.equals(空))
            this.yearHigh =N / A;
        其他
            this.yearHigh = yearHigh;
    }
    公共字符串getYearLow(){
        返回yearLow;
    }
    公共无效setYearLow(字符串yearLow){
        如果(yearLow.equals(空))
            this.yearLow =N / A;
        其他
            this.yearLow = yearLow;
    }    公共字符串getLastTradePriceOnly(){
        返回lastTradePriceOnly;
    }    公共无效setLastTradePriceOnly(字符串lastTradePriceOnly){
        如果(lastTradePriceOnly.equals(空)){
            this.lastTradePriceOnly =N / A;
        }
        其他{
            浮floatedLastTradePriceOnly = Float.valueOf(lastTradePriceOnly);
            this.lastTradePriceOnly =(df.format(floatedLastTradePriceOnly));
        }
    }    @覆盖
    公众诠释哈希code(){
        最终诠释黄金= 31;
        INT结果= 1;
        结果=黄金*结果+((其他城市== NULL)?0:change.hash code());
        结果=黄金*结果
                +((daysHigh == NULL)?0:daysHigh.hash code());
        结果=黄金*结果+((daysLow == NULL)?0:daysLow.hash code());
        结果=总理
                *结果
                +((lastTradePriceOnly == NULL)?0:lastTradePriceOnly
                        .hash code());
        结果=总理
                *结果
                +((marketCapitalization == NULL)?0:marketCapitalization
                        .hash code());
        结果=黄金*结果+((名称== NULL)0:name.hash code());
        结果=黄金*结果+((开== NULL)?0:open.hash code());
        结果=黄金*结果+((操作== NULL)?0:peRatio.hash code());
        结果=黄金*结果
                +((percentChange == NULL)0:percentChange.hash code());
        结果=黄金*结果+((符号== NULL)?0:symbol.hash code());
        结果=黄金*结果+((体积== NULL)?0:volume.hash code());
        结果=黄金*结果
                +((yearHigh == NULL)?0:yearHigh.hash code());
        结果=黄金*结果+((yearLow == NULL)?0:yearLow.hash code());
        返回结果;
    }
    @覆盖
    公共布尔等于(obj对象){
        如果(这种== OBJ)
            返回true;
        如果(OBJ == NULL)
            返回false;
        如果(的getClass()!= obj.getClass())
            返回false;
        报价等=(行情)目标文件;
        如果(改== NULL){
            如果(other.change!= NULL)
                返回false;
        }否则如果(!change.equals(other.change))
            返回false;
        如果(daysHigh == NULL){
            如果(other.daysHigh!= NULL)
                返回false;
        }否则如果(!daysHigh.equals(other.daysHigh))
            返回false;
        如果(daysLow == NULL){
            如果(other.daysLow!= NULL)
                返回false;
        }否则如果(!daysLow.equals(other.daysLow))
            返回false;
        如果(lastTradePriceOnly == NULL){
            如果(other.lastTradePriceOnly!= NULL)
                返回false;
        }否则如果(!lastTradePriceOnly.equals(other.lastTradePriceOnly))
            返回false;
        如果(marketCapitalization == NULL){
            如果(other.marketCapitalization!= NULL)
                返回false;
        }否则如果(!marketCapitalization.equals(other.marketCapitalization))
            返回false;
        如果(名称== NULL){
            如果(other.name!= NULL)
                返回false;
        }否则如果(!name.equals(other.name))
            返回false;
        如果(开== NULL){
            如果(other.open!= NULL)
                返回false;
        }否则如果(!open.equals(other.open))
            返回false;
        如果(操作== NULL){
            如果(other.peRatio!= NULL)
                返回false;
        }否则如果(!peRatio.equals(other.peRatio))
            返回false;
        如果(percentChange == NULL){
            如果(other.percentChange!= NULL)
                返回false;
        }否则如果(!percentChange.equals(other.percentChange))
            返回false;
        如果(符号== NULL){
            如果(other.symbol!= NULL)
                返回false;
        }否则如果(!symbol.equals(other.symbol))
            返回false;
        如果(卷== NULL){
            如果(other.volume!= NULL)
                返回false;
        }否则如果(!volume.equals(other.volume))
            返回false;
        如果(yearHigh == NULL){
            如果(other.yearHigh!= NULL)
                返回false;
        }否则如果(!yearHigh.equals(other.yearHigh))
            返回false;
        如果(yearLow == NULL){
            如果(other.yearLow!= NULL)
                返回false;
        }否则如果(!yearLow.equals(other.yearLow))
            返回false;
        返回true;
    }
}

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

 私人无效serializeQuotes(){
        FileOutputStream中FOS;
        尝试{
            FOS = openFileOutput(Constants.FILENAME,Context.MODE_PRIVATE);
            ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);
            oos.writeObject(引号); //这是一个ArrayList<&报价GT;
            oos.close();
        }赶上(FileNotFoundException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }


解决方案

你为什么要在所有的序列化 DecimalFormat的?替换实例 DecimalFormat的字段静态的。如果并发性是一个问题,使用静态的ThreadLocal<&DecimalFormat的GT; 秒。

 公共类报价实现Serializable {    私有静态最后的serialVersionUID长1L =;    私人字符串符号;
    私人字符串名称;
    私人字符串的变化;
    私人字符串percentChange;
    私人字符串打开;
    私人字符串daysHigh;
    私人字符串daysLow;
    私人字符串dividendYield;
    私人字符串量;
    私人字符串averageDailyVolume;
    私人字符串操作;
    私人字符串marketCapitalization;
    私人字符串yearHigh;
    私人字符串yearLow;
    私人字符串lastTradePriceOnly;
    私有静态最后的ThreadLocal<&DecimalFormat的GT; DF =新的ThreadLocal<&DecimalFormat的GT; {
        @覆盖
        保护DecimalFormat的初值(){
            返回新DecimalFormat的(#,###,###,###,###,## 0.00);
        }
    }    私有静态最后的ThreadLocal<&DecimalFormat的GT; VF =新的ThreadLocal<&DecimalFormat的GT; {
        @覆盖
        保护DecimalFormat的初值(){
            返回新DDecimalFormat(#,###,###,###,###,## 0);
        }
    }    //略...}

http://download.oracle.com/ JavaSE的/ 6 /文档/ API / JAVA / LANG / ThreadLocal.html

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

解决方案

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

这篇关于问题2.3.4序列化的DecimalFormatSymbols的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆