问题描述
如果我尝试写
Car myCarObject=getCar();
Intent details = new Intent(Start.this, DetailsCar.class);
details.putExtra("Car", myCarObject);
startActivity(details);
Eclipse的告诉我一个编译错误的投入额外的方法是不明确的类型意图的行
details.putExtra("Car", myCarObject);
如果我用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);
和我尝试加载额外的这个code在目标意图与
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
我的车对象是Parcelable
My Car object is Parcelable
所以....有什么不好?
so.... what is wrong?
推荐答案
第一个错误:投入额外的方法是不明确的类型意图
类汽车既是序列化和Parcelable,编译器不知道是否使用putExtra(序列化S)或putExtra(Parcelable P)来处理您的请求。所以,你必须使用Intent.putExtra()时,投你的车到其中之一。
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);
第二个错误:java.lang.ClassCastException:java.util.ArrayList中
您把车子对象的ArrayList和使用putExtra发送到下一个活动。一个ArrayList是不是Parcelable但只可序列化。该putExtra(ArrayList中)的工作原理putExtra(序列化),但您可以通过getParcelable阅读()。一个ArrayList不能被转换为Parcelable。
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.
这篇关于无法通过自定义对象在意图:把额外的方法是不明确的类型意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!