Reputation: 115
I'm working in the android Studio,2.1.2.
I have a mysterious cast Exception between two activity.
In the First Activity i have this ArrayList:
ArrayList<riparazione> nuovo = notizia.get_Elenco();
I insert the arraylist ( parcellable ) in a bundle for send at another activity:
Intent intent = new Intent(prenota_mostra_cellulari.this, prenota_mostra_dettagli.class);
Bundle spedizione = new Bundle();
spedizione.putString("Nome",notizia.get_Marca());
spedizione.putString("Foto",notizia.get_Foto());
spedizione.putString("Marca",notizia.get_Nome());
spedizione.putParcelableArrayList("Riparazioni",nuovo);
intent.putExtras(spedizione);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
In second activity i use this for recover ArrayList
ArrayList<riparazione> nuovo = new ArrayList<>();
Intent intent = getIntent();
if (null != intent) {
dati_cel = intent.getExtras();
nuovo = dati_cel.getParcelableArrayList("Riparazioni");
Log.d("Dati", String.valueOf(dati_cel.size()));
Log.d("Dati arraylist", String.valueOf(dati_cel.size()));
riparazione dads = nuovo.get(9);
}
NOW in line of riparazione dads = nuovo.get(9);
i get an Cast error :
Caused by: java.lang.ClassCastException: com.example.luca.ireplace.orario cannot be cast to com.example.luca.ireplace.riparazione
I have tried everything , with and without Bundle for sending, the class output end output are the same why this casting? And How do I solve??
Thanks for any Help
Upvotes: 0
Views: 129
Reputation: 3501
The error is not in the code that you posted. Remember that Lists (and generics generally) suffer from type erasure. That means that even though you declared nuovo as ArrayList<riparazione>
, Java will not check at runtime whether you are adding riparazione
s to the list or whether the list actually contains riparazione
s when you pass it.
You somehow inserted an orario
into the list. The error occurs here:
riparazione dads = nuovo.get(9);
We have no idea how the nuovo
list originally gets constructed, but the error is obviously not in the code that you posted.
Upvotes: 1