无法在Intent中传递自定义对象:对于类型Intent,Method Put Extra是不明确的

时间:2012-12-14 14:16:14

标签: android android-intent parcelable

如果我试着写

Car myCarObject=getCar();
Intent details = new Intent(Start.this, DetailsCar.class);
                details.putExtra("Car", myCarObject);
                startActivity(details);

Eclipse向我显示编译错误“方法放置额外对于类型Intent是不明确的”在行

  details.putExtra("Car", myCarObject);

如果我使用代码

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

我尝试使用此代码在目标Intent中加载额外的

ArrayList<Car> parcelableExtra = new ArrayList<Car>();
        parcelableExtra = (ArrayList<Car>) getIntent().getExtras().getParcelable("Car");
        Car c=parcelableExtra.get(0);

我收到此警告

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)

应用程序因Null Point异常而崩溃

我的Car对象是Parcelable

所以......出了什么问题?

5 个答案:

答案 0 :(得分:67)

第一个错误:&#39;对于Intent&#39;。

类型,方法放置额外是不明确的

课程Car同时为SerializableParcelable,编译器不知道是使用putExtra(Serializable s)还是putExtra(Parcelable p)来处理您的请求。因此,在使用Car时,您必须将Intent.putExtra()投射到其中一个。

Intent.putExtra("car", (Parcelable)myCarObject);
Intent.putExtra("car", (Serializable)myCarObject);

第二个错误:java.lang.ClassCastException:java.util.ArrayList

您将Car对象放在ArrayList中并使用putExtra发送到下一个活动。 ArrayList不是Parcelable,而只是SerializableputExtra(ArrayList)的作用为putExtra(Serializable),但您可以通过getParcelable()阅读。 ArrayList无法投放到Parcelable

答案 1 :(得分:24)

我用这个

在发件人活动中

Intent intent = new Intent(activity, MyActivity.class);

Bundle bundle = new Bundle();
bundle.putSerializable("my object", myObject);

intent.putExtras(bundle);

startActivity(intent);

在收件人中:

myObject = (MyObject) getIntent().getExtras().getSerializable("my object");

适合我的工作尝试。但该对象必须是可序列化的:)

答案 2 :(得分:13)

这是我传递可序列化对象的方式,我相信它应该以相同的方式用于parcelable。 通过:

Intent intent=new Intent(OverviewActivity.this,CarDetailTabActivity.class);         
            intent.putExtra("CAR",myCarObject);
            startActivity(intent);  

接收:

Car carObject=(Car)getIntent().getSerializableExtra("CAR");

汽车:

import java.io.Serializable;

public class Car implements Serializable {

private static final long serialVersionUID = 1L;
......

答案 3 :(得分:2)

getIntent().getExtras()会从您的意图中返回额外的Bundle,而不是您的数据。要获取列表,请使用getIntent().getParcelableArrayListExtra("Car")

答案 4 :(得分:2)

解决方案:由您的类(例如Serializable

)实现implements Serializable接口