在 Intent 中传 SparseArray 类型的数据
2016-04-20 20:44
615 查看
Intent 传递对象
Android 中Intent传递类对象提供了两种方式一种是通过 实现Serializable接口 传递对象,一种是通过 实现Parcelable接口 传递对象。要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。
Intent中传递这2种对象的方法:
Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象
SparseArray(稀疏数组)
项目中因为有用到 SparseArray 代替 HashMap 来进行性能优化。结果在两个 Activity 之间用 Intent 传递 一个包含 SparseArray 类型数据的对象(该对象实现了 Serializable 接口) 时报了以下异常:
Parcelable encountered IOException writing serializable object
原因:
在使用 Intent 传递 serializable类型的对象时,必须保证该对象中的所有数据内容都实现serializable接口。
而我传递的对象中的 SparseArray 没有实现serializable接口(一些String、Int等基本数据类型已实现serializable接口)。
解决方案:
自定义 SparseArray 实现 serializable 接口 ,来替代 原有的SparseArray 。
代码如下:
import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import android.util.SparseArray; /** * @author Asaf Pinhassi www.mobiledev.co.il * @param <E> * */ public class SerializableSparseArray<E> extends SparseArray<E> implements Serializable{ private static final long serialVersionUID = 824056059663678000L; public SerializableSparseArray(int capacity){ super(capacity); } public SerializableSparseArray(){ super(); } /** * This method is private but it is called using reflection by java * serialization mechanism. It overwrites the default object serialization. * * <br/><br/><b>IMPORTANT</b> * The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException} * will be thrown. * * @param oos * the stream the data is stored into * @throws IOException * an exception that might occur during data storing */ private void writeObject(ObjectOutputStream oos) throws IOException { Object[] data = new Object[size()]; for (int i=data.length-1;i>=0;i--){ Object[] pair = {keyAt(i),valueAt(i)}; data[i] = pair; } oos.writeObject(data); } /** * This method is private but it is called using reflection by java * serialization mechanism. It overwrites the default object serialization. * * <br/><br/><b>IMPORTANT</b> * The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException} * will be thrown. * * @param oos * the stream the data is read from * @throws IOException * an exception that might occur during data reading * @throws ClassNotFoundException * this exception will be raised when a class is read that is * not known to the current ClassLoader */ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { Object[] data = (Object[]) ois.readObject(); for (int i=data.length-1;i>=0;i--){ Object[] pair = (Object[]) data[i]; this.append((Integer)pair[0],(E)pair[1]); } return; } }
参考文章:
/article/4921791.html
http://stackoverflow.com/questions/14899718/how-to-store-sparsearray-in-bundle
相关文章推荐
- 20135327郭皓--Linux内核分析第九周 期中总结
- Java Collections Framework概览
- python核心编程第二版 第8章课后题
- java泛型的理解
- php面向对象——类和对象
- HDOJ 1116 Play on Words
- PHP Fatal error: Call to a member function fetch_all() on a non-object问题的解决
- ActivityManagerService 笔记
- dubbo远程调试运行
- java final
- RHEL7安装部署ZooKeeper
- hdu 2546 饭卡 01背包问题
- iOS笔记UI--使用storyboard添加约束
- 团队项目-BUG挖掘
- 构建之法阅读笔记06
- 初识MFC学习——Hello World
- Web前端性能优化
- 1001 Can you solve this equation?
- 编辑工具Sublime Text 3 使用技巧
- java 复制复杂链表