无法通过自定义对象在意图:把额外的方法是不明确的型意图(Cannot pass custom Obj

2019-07-05 07:43发布

如果我尝试写

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

ECLIPSE告诉我编译错误“额外放方法是暧昧的型意图”在该行

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

我尝试加载额外的与此代码的目的与意图

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)

而应用程序崩溃与零点异常

我的车对象Parcelable

所以....什么是错的?

Answer 1:

第一个错误:“额外放方法是暧昧的型意图”。

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但只Serializable 。 该putExtra(ArrayList)可以作为putExtra(Serializable) ,但你通过阅读getParcelable() 一个ArrayList不能被转换到Parcelable



Answer 2:

我用这个

在发件人Activiy

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

工作正常,我试试吧。 但对象必须是可序列化:)



Answer 3:

这是我通过我的对象是哪些序列化,我认为它应该工作的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;
......


Answer 4:

getIntent().getExtras()返回额外的Bundle从你的意图,而不是你的数据。 为了让您的列表中使用getIntent().getParcelableArrayListExtra("Car")



Answer 5:

解决方案 :实现Serializable的类一样的界面implements Serializable



文章来源: Cannot pass custom Object in an Intent: The Method Put Extra is Ambiguous for the type Intent