android: problem with Serializable object put into intent

自闭症网瘾萝莉.ら 提交于 2019-12-18 05:06:06

问题


Hi i have problem with a class i want to pass in an intent by putting it into the putExtras() Its serializable and the code looks like this:

public abstract class ObjectA extends ArrayList<ObjectA> implements java.io.Serializable{...}

public class ObjectB extends ObjectA {...}


...
Bundle extras = new Bundle();
extras.putSerializable("blabla", ObjectB);
intent.putExtras(extras);

...

Object y = getIntent().getExtras().get("blabla");

the problem is, that y now is an ArrayList and no longer an ObjectB so i cant cast it.. if i change the code to

public class ObjectB implements java.io.Serializable {...}

it works fine


回答1:


By implementing both java.util.List and java.io.Serializable in your class you've triggered this android bug.




回答2:


I suspect what's happening is that since you aren't declaring ObjectB as serializable it's "falling back" to the most recent parent object that is. So when you put it in to the Bundle you aren't actually putting in ObjectB, but ObjectB cast back to ArrayList.

I think you're going to have to go with the second ("works fine") code.




回答3:


How are you declaring ObjectB before you pass it into the bundle? From what I understand of your question you are getting no error when you pass in the bundle, only when removing it. Try retreiving your ObjectB into an ObjectB type directly like this:

ObjectB y = (ObjectB) getIntent().getExtras().get("blabla");



回答4:


The ObjectA in ArrayList should implements the interface Parcelable. After that you can put your arraylists in the intent, and get them in another activity.



来源:https://stackoverflow.com/questions/1548489/android-problem-with-serializable-object-put-into-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!