search

Home  >  Q&A  >  body text

android - Activity之间传递Map<String,Object>

listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Intent it = new Intent(getActivity(), ReleaseDetail.class);
            final SerializableMap myMap = new SerializableMap();
            myMap.setMap(tempList);// 将map数据添加到封装的myMap中
            Bundle bundle = new Bundle();
            bundle.putSerializable("map1", myMap);
            it.putExtras(bundle);
            startActivity(it);
            getActivity().finish();
        }
    });

public class SerializableMap implements Serializable {

private static final long serialVersionUID = 3958588986554810147L;
private Map<String, ReleaseContents> map;

public Map<String, ReleaseContents> getMap() {
    return map;
}

public void setMap(Map<String, ReleaseContents> tempMap) {
    this.map = tempMap;
}

}

ReleaseContents是一个实体类也实现了Serializable 接口还是报以下错误呢!!!

07-12 12:51:58.093: E/AndroidRuntime(12270): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = XXX.SerializableMap)XXX
查资料说是对象也是要先序列化接口,我ReleaseContents都实现了的呀!请问是哪里错了哦!

迷茫迷茫2772 days ago632

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 17:33:33

    The classes used under the ReleaseContents class also need to implement the Serializable interface. For example, your ReleaseContents class has an attribute that is Version< /code> class, then this Version class may implement the Serializable interface. ReleaseContents类下使用到的类也需要实现Serializable接口,例如你的ReleaseContents类里面有个属性是Version类,那么这个Version类也许实现Serializable接口。

    回到你的例子,很明显,在ReleaseContents类有个属性是Bitmap类,Bitmap并没有实现Serializable接口,而是实现了Parcelable

    Back to your example, it is obvious that the ReleaseContents class has an attribute of the Bitmap class, and Bitmap does not implement Serializable< /code>interface, but implements the Parcelable interface. 🎜

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:33:33

    Use Android's serialization class instead of the traditional java serialization class
    Parcelable this. Or use the Gson tool to convert the map into a String, then pass it and parse it into a map

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:33:33

    map没有实现序列化的接口,无法实现序列化,可以尝试一下hashmaphashmap原本就可以保存在bundle中,也可像楼上一样使用parcelableIt is also faster to achieve this.

    reply
    0
  • 阿神

    阿神2017-04-17 17:33:33

    Use a CacheUtil to save this map in memory

    reply
    0
  • Cancelreply