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

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

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 parcelableExtra = new ArrayList();                 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 parcelableExtra = new ArrayList();         parcelableExtra = (ArrayList) 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  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.        
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!