Downcasting from Serializable to LinkedList <classes>

I am trying to send a LinkedList from Activity to another. In the first Office, I:

 LinkedList<Class> codaActivity; /* Lots of code here... */ Intent intent = new Intent(this, codaActivity.remove()); intent.putExtra("codaRedditi", codaActivity); // this is putExtra(String, Serializable) startActivity(intent); 

Instead of the second:

 // The following throws a ClassCastException codaRedditi=(LinkedList<Class>) (getIntent().getSerializableExtra("codaRedditi")); 

When I try to run my application, DVM throws a ClassCastException caused by this code (and talks about an ArrayList that absolutely does not exist in the code! OO)

What could be a mistake?

+4
source share
3 answers

Are you sure that the goal you are addressing is the one you created? Try debugging the intent, for example, with the output:

getIntent().getSerializableExtra("codaRedditi").getClass()

or the object itself:

getIntent().getSerializableExtra("codaRedditi")

What are you getting?

Are you also sure that you are using the right LinkedList , where are you doing the cast? Look at the import if it declares: java.util.LinkedList

+2
source

Are you sure intent is an instance of LinkedList ?

Attempt:

 codaRedditi=new LinkedList((List)(getIntent().getSerializableExtra("codaRedditi"))); 
0
source

From the docs:

 public Intent putExtra (String name, Serializable value) Since: API Level 1 Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll". Parameters name The name of the extra data, with package prefix. value The Serializable data value. 

It says that the name field must have a package prefix, for example, com.blah.something. Perhaps this is causing the problem.

0
source

Source: https://habr.com/ru/post/1438259/


All Articles