Cannot pass custom Object in an Intent: The Method

2019-01-14 10:38发布

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?

5条回答
一纸荒年 Trace。
2楼-- · 2019-01-14 11:06

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.

查看更多
再贱就再见
3楼-- · 2019-01-14 11:11

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-14 11:18

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;
......
查看更多
来,给爷笑一个
5楼-- · 2019-01-14 11:19

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

查看更多
forever°为你锁心
6楼-- · 2019-01-14 11:27

Solution : implement Serializable interface by your class like implements Serializable

查看更多
登录 后发表回答