gpt4 book ai didi

android - 无法在 Intent : The Method Put Extra is Ambiguous for the type Intent 中传递自定义对象

转载 作者:IT王子 更新时间:2023-10-29 00:07:57 24 4
gpt4 key购买 nike

如果我尝试写作

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

Eclipse 向我显示编译错误“The Method Put Extra is Ambiguous for the type 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)

应用程序因空点异常而崩溃

我的 Car 对象是 Parcelable

所以....怎么了?

最佳答案

第一个错误:'The Method Put Extra is Ambiguous for the type Intent'。

Car类既是Serializable又是Parcelable,编译器不知道是否使用putExtra(Serializable s) putExtra(Parcelable p) 来处理您的请求。因此,在使用 Intent.putExtra() 时,您必须将您的 Car 转换为其中之一。

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

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

您将 Car 对象放入 ArrayList 并使用 putExtra 发送到下一个 Activity 。 ArrayList 不是 Parcelable 而只是 SerializableputExtra(ArrayList)putExtra(Serializable) 一样工作,但您可以通过 getParcelable() 读取它。 ArrayList 不能转换为 Parcelable

关于android - 无法在 Intent : The Method Put Extra is Ambiguous for the type Intent 中传递自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com