4

I'm trying to send a LinkedList from an Activity to an other one. In the first Activity I have:

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

In the second one, instead:

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

When I try to run my app, the DVM throws a ClassCastException caused by that code (and talking about an ArrayList, that absolutely does't exists in the code! O.O)

What can be the mistake?

3 Answers3

2

Are you sure that the intent you are accessing is the one you have created? Try to debug the intent, for instance with outputting:

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

or the object itself:

getIntent().getSerializableExtra("codaRedditi")

What do you receive?

Also are you sure you are using the right LinkedList where you do the cast? Look into the imports, does it state: java.util.LinkedList

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
0

Are you sure intent is instance of LinkedList?

Trying:

codaRedditi=new LinkedList((List)(getIntent().getSerializableExtra("codaRedditi")));
Koerr
  • 15,215
  • 28
  • 78
  • 108
  • No, Intent isn't a LinkedList, but Intent.getSerializedExtra() returns a Serializable, that LinkedList is. I've tried your code, but Eclipse reminds me that LinkedList hasn't a constructor with a List parameter... – user1726800 Oct 07 '12 at 17:11
  • @user1726800 But it [does](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html#LinkedList(java.util.Collection)! Are you sure you are using `java.util.LinkedList` for the cast, respectively in the declaration? – Konrad Reiche Oct 07 '12 at 19:14
  • http://developer.android.com/reference/java/util/LinkedList.html#LinkedList%28%29 Yes I am. Here I can see only an emty constructor and another one, that requires a Collection, that isn´t a List..! – user1726800 Oct 08 '12 at 09:14
0

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, the "name" field" should have package prefix like 'com.blah.something". Maybe this is causing the problem.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84