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 both Serializable
and Parcelable
, the compiler doesn't know whether to use putExtra(Serializable s)
or putExtra(Parcelable p)
to handle your request. So you have to cast your Car
to one of them when using Intent.putExtra()
.
Intent.putExtra("car", (Parcelable)myCarObject);
Intent.putExtra("car", (Serializable)myCarObject);
The second error: java.lang.ClassCastException: java.util.ArrayList
You put the Car
object in a ArrayList
and use putExtra
to send to the next activity. An ArrayList
is not Parcelable
but only Serializable
. The putExtra(ArrayList)
works as putExtra(Serializable)
, but you read it by getParcelable()
. An ArrayList
cannot be cast to Parcelable
.
I use this
In the sender Activiy
Intent intent = new Intent(activity, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("my object", myObject);
intent.putExtras(bundle);
startActivity(intent);
In the receiver:
myObject = (MyObject) getIntent().getExtras().getSerializable("my object");
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:
Intent intent=new Intent(OverviewActivity.this,CarDetailTabActivity.class);
intent.putExtra("CAR",myCarObject);
startActivity(intent);
Receive:
Car carObject=(Car)getIntent().getSerializableExtra("CAR");
Car:
import java.io.Serializable;
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
......
Solution : implement Serializable
interface by your class like implements Serializable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With