views:

91

answers:

2

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?

A: 

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]);
        }
    }
}
Pentium10
+1  A: 

I'm sorry; I overlooked the use of the no-arg toArray() call.

Please note that there's overloaded toArray(T[]) method that takes an array as an argument.

By using this form, you can control the component type of the array, and it will work as expected.

protected void onPostExecute(SortedSet<RatedMessage> result) {
  lancon.putExtra("results", result.toArray(new RatedMessage[result.size()]));
}
erickson