cuestión serialización con SortedSet, matrices, una Serializable
-
01-10-2019 - |
Pregunta
Tengo esto antes de que el proceso:
protected void onPostExecute(SortedSet<RatedMessage> result) {
List<Object> list=Arrays.asList(result.toArray());
lancon.putExtra("results", list.toArray()); // as serializable
}
A continuación, en la otra parte tengo
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);
¿Por qué recibo el ClassCastException?
Solución
Finalmente conseguí que funcione de esta manera:
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]);
}
}
}
Otros consejos
Lo siento; Pasé por alto el uso de la llamada toArray()
sin argumentos.
Tenga en cuenta que hay sobrecargado método toArray(T[])
que toma una matriz como argumento.
Mediante el uso de esta forma, se puede controlar el tipo de componente de la matriz, y funcionará como se esperaba.
protected void onPostExecute(SortedSet<RatedMessage> result) {
lancon.putExtra("results", result.toArray(new RatedMessage[result.size()]));
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow