Cannot pass custom Object in an Intent: The Method Put Extra is Ambiguous for the type Intent
Asked Answered
L

5

42

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?

Lamoreaux answered 14/12, 2012 at 14:16 Comment(0)
R
89

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.

Roomy answered 14/12, 2012 at 14:42 Comment(1)
In Kotlin: myCarObject as Parcelable or myCarObject as SerializableBothnia
C
28

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

Carob answered 1/8, 2013 at 11:49 Comment(0)
S
13

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;
......
Strapper answered 14/12, 2012 at 14:26 Comment(0)
C
2

Solution : implement Serializable interface by your class like implements Serializable

Crayon answered 5/7, 2018 at 10:28 Comment(0)
P
1

getIntent().getExtras() returns extra Bundle from your intent, not your data. To get your list use getIntent().getParcelableArrayListExtra("Car")

Parasiticide answered 14/12, 2012 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.