Search code examples
javalistserializationsetsortedset

Serialization issue with SortedSet, Arrays, an Serializable


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
}

then in the other part I have

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

Why I get the ClassCastException?


Solution

  • 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]);
            }
        }
    }