If I try to write
Car myCarObject=getCar();
Intent details = new Intent(Start.this, DetailsCar.class);
details.putExtra("Car", myCarObject);
startActivity(details);
Eclipse show me a compilation error "The Method Put Extra is Ambiguous for the type Intent" in the line
details.putExtra("Car", myCarObject);
If I use the code
Car myCarObject=getCar();
ArrayList<Car> parcelableExtra = new ArrayList<Car>();
parcelableExtra.add(myCarObject);
Intent details = new Intent(Start.this, DetailsCar.class);
details.putExtra("Car", parcelableExtra);
startActivity(dettagli);
And I try to load the extra with this code in the destination Intent with
ArrayList<Car> parcelableExtra = new ArrayList<Car>();
parcelableExtra = (ArrayList<Car>) getIntent().getExtras().getParcelable("Car");
Car c=parcelableExtra.get(0);
I get this warning
12-14 05:30:07.669: W/Bundle(19823): Key Car expected Parcelable but value was a java.util.ArrayList. The default value <null> was returned.
12-14 05:30:07.679: W/Bundle(19823): Attempt to cast generated internal exception:
12-14 05:30:07.679: W/Bundle(19823): java.lang.ClassCastException: java.util.ArrayList
12-14 05:30:07.679: W/Bundle(19823): at android.os.Bundle.getParcelable(Bundle.java:1106)
12-14 05:30:07.679: W/Bundle(19823): at my.app.com.DetailsCar.onCreate(DetailsCar.java:43)
12-14 05:30:07.679: W/Bundle(19823): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-14 05:30:07.679: W/Bundle(19823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-14 05:30:07.679: W/Bundle(19823): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-14 05:30:07.679: W/Bundle(19823): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-14 05:30:07.679: W/Bundle(19823): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-14 05:30:07.679: W/Bundle(19823): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 05:30:07.679: W/Bundle(19823): at android.os.Looper.loop(Looper.java:130)
12-14 05:30:07.679: W/Bundle(19823): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-14 05:30:07.679: W/Bundle(19823): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 05:30:07.679: W/Bundle(19823): at java.lang.reflect.Method.invoke(Method.java:507)
12-14 05:30:07.679: W/Bundle(19823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-14 05:30:07.679: W/Bundle(19823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-14 05:30:07.679: W/Bundle(19823): at dalvik.system.NativeStart.main(Native Method)
12-14 05:30:07.679: W/dalvikvm(19823): threadid=1: thread exiting with uncaught exception (group=0x40018578)
And the app crashes with a Null Point Exception
My Car object is Parcelable
so.... what is wrong?
The first error: 'The Method Put Extra is Ambiguous for the type Intent'.
The class
Car
is bothSerializable
andParcelable
, the compiler doesn't know whether to useputExtra(Serializable s)
orputExtra(Parcelable p)
to handle your request. So you have to cast yourCar
to one of them when usingIntent.putExtra()
.The second error: java.lang.ClassCastException: java.util.ArrayList
You put the
Car
object in aArrayList
and useputExtra
to send to the next activity. AnArrayList
is notParcelable
but onlySerializable
. TheputExtra(ArrayList)
works asputExtra(Serializable)
, but you read it bygetParcelable()
. AnArrayList
cannot be cast toParcelable
.I use this
In the sender Activiy
In the receiver:
Works fine for me try it. But the object must be serializable :)
This is how I pass my objects which are serializable, I believe it should work the same way for parcelable. Pass:
Receive:
Car:
getIntent().getExtras()
returns extraBundle
from your intent, not your data. To get your list usegetIntent().getParcelableArrayListExtra("Car")
Solution : implement
Serializable
interface by your class likeimplements Serializable