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

问题描述

在处理之前,我已经准备好了

I have this before the process:

protected void onPostExecute(SortedSet<RatedMessage> result) {
    List<Object> list=Arrays.asList(result.toArray());
    lancon.putExtra("results", list.toArray()); // as serializable
}

然后我有另一部分

Object o=this.getIntent().getSerializableExtra("results");
//at this point the o holds the correct value (checked by debugger)
RatedMessage[] rm = (RatedMessage[]) o;// this line hangs out w ClassCastException
resultSet = new TreeSet<RatedMessage>(new Comp());
Collections.addAll(resultSet, rm);

为什么会收到ClassCastException?

Why I get the ClassCastException?

推荐答案

最后,我使它以这种方式工作:

Finally I got it to work this way:

Serializable s = this.getIntent().getSerializableExtra("results");
Object[] o = (Object[]) s;
if (o != null) {
    resultSet = new TreeSet<RatedMessage>(new Comp());
    for (int i = 0; i < o.length; i++) {
        if (o[i] instanceof RatedMessage) {
            resultSet.add((RatedMessage) o[i]);
        }
    }
}

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

08-14 02:06