android java.io.NotSerializableException序列化异常
2016-07-07 19:32
471 查看
今天被这个异常坑了好几个小时。首先把我的需求说下。。今天要开发一个底部Tabbar。但是Tabbar的数据和数量都是在服务端传过来,然后我们在通过数据绘制Tabbar,
绘制完后需要把这段信息保存序列化保存在本地。以免下次网络不好的时候,我们而已直接在本地拿数据来绘制Tabbar。
好了,搞了2个小时。代码全部写好了,准备开始测试的时候,在序列化对象的时候一直出爆错
java.io.NotSerializableException:
,,没错,就是一直爆错
java.io.NotSerializableException:
。。。。。。
大家发现了一个什么问题了没有。我明明序列化的是TabbarEntity。为什么会告诉我无法序列化,TabbarData这个类呢。搞了好久看了源代码才发现,原来这个序列化有一个毛病。。就是在序列化之前不能被其他对象间接或者直接引用,否者就会连使用到的被使用的那个对象也一并序列化。。
--------------------------------下面贴一段我的代码
<span style="font-size:18px;">TabbarEntity tabbarEntity=new TabbarEntity();
List<TabbarEntity.AppTabListBean> tabList=new ArrayList<TabbarEntity.AppTabListBean>(){};
for (int i = 0; i < 4; i++) {
//构造显示数据
TabbarEntity.AppTabListBean appTabBean=new TabbarEntity.AppTabListBean();
appTabBean.setTabTitle(defaultText[i]);
appTabBean.setTabIcon(images_unselected[i] + "");
appTabBean.setTabSelectIcon(image_selected[i] + "");
appTabBean.setTabUrl(default_tabbar_url[i]);
tabList.add(appTabBean);
}
<span style="white-space:pre"> </span>把下面的红色加升的序列化代码移到这里。。好了解决了<span style="white-space:pre"> </span></span><pre name="code" class="java"><span style="font-size:24px;color:#ff0000;"><span style="white-space:pre"> </span><span style="background-color: rgb(51, 204, 0);">serializeTabbarData(tabbarEntity);</span></span> //构建url和bitmap String[] url=new String[tabList.size()]; Bitmap[] selectIcon=new Bitmap[tabList.size()]; Bitmap[] unSelectIcon=new Bitmap[tabList.size()]; for (int i = 0; i <tabList.size(); i++) { url[i]=tabList.get(i).getTabUrl(); selectIcon[i]=
BitMapUtil.getBitmapToResource(context,Integer.parseInt(tabList.get(i).getTabIcon())); unSelectIcon[i] = BitMapUtil.getBitmapToResource(context,Integer.parseInt(tabList.get(i).getTabSelectIcon())); } //放进底部实例 BottomViewItem.getInstance().setUrl(url); BottomViewItem.getInstance().setSelectIcon(selectIcon);
BottomViewItem.getInstance().setUnSelectIcon(unSelectIcon);
//这里序列化的时候,在上面已经被BottomViewItem,和Bitmap引用到了,所以在序列化的时候也会一并把其他都序列化了。
serializeTabbarData(tabbarEntity); tabbarEntity.setAppTabList(tabList);
---------------------------------
然后来总结下。。
下次在序列化对象的时候,一定要在对象构造完后。先序列化,在被其他对象引用里面的值,否者他们就会有关联关系,。。。。。
绘制完后需要把这段信息保存序列化保存在本地。以免下次网络不好的时候,我们而已直接在本地拿数据来绘制Tabbar。
好了,搞了2个小时。代码全部写好了,准备开始测试的时候,在序列化对象的时候一直出爆错
java.io.NotSerializableException:
com.logansoft.zcbao.fragment.menu.control.TabbarData;
,,没错,就是一直爆错
java.io.NotSerializableException:
com.logansoft.zcbao.fragment.menu.control.TabbarData;
。。。。。。
大家发现了一个什么问题了没有。我明明序列化的是TabbarEntity。为什么会告诉我无法序列化,TabbarData这个类呢。搞了好久看了源代码才发现,原来这个序列化有一个毛病。。就是在序列化之前不能被其他对象间接或者直接引用,否者就会连使用到的被使用的那个对象也一并序列化。。
--------------------------------下面贴一段我的代码
<span style="font-size:18px;">TabbarEntity tabbarEntity=new TabbarEntity();
List<TabbarEntity.AppTabListBean> tabList=new ArrayList<TabbarEntity.AppTabListBean>(){};
for (int i = 0; i < 4; i++) {
//构造显示数据
TabbarEntity.AppTabListBean appTabBean=new TabbarEntity.AppTabListBean();
appTabBean.setTabTitle(defaultText[i]);
appTabBean.setTabIcon(images_unselected[i] + "");
appTabBean.setTabSelectIcon(image_selected[i] + "");
appTabBean.setTabUrl(default_tabbar_url[i]);
tabList.add(appTabBean);
}
<span style="white-space:pre"> </span>把下面的红色加升的序列化代码移到这里。。好了解决了<span style="white-space:pre"> </span></span><pre name="code" class="java"><span style="font-size:24px;color:#ff0000;"><span style="white-space:pre"> </span><span style="background-color: rgb(51, 204, 0);">serializeTabbarData(tabbarEntity);</span></span> //构建url和bitmap String[] url=new String[tabList.size()]; Bitmap[] selectIcon=new Bitmap[tabList.size()]; Bitmap[] unSelectIcon=new Bitmap[tabList.size()]; for (int i = 0; i <tabList.size(); i++) { url[i]=tabList.get(i).getTabUrl(); selectIcon[i]=
BitMapUtil.getBitmapToResource(context,Integer.parseInt(tabList.get(i).getTabIcon())); unSelectIcon[i] = BitMapUtil.getBitmapToResource(context,Integer.parseInt(tabList.get(i).getTabSelectIcon())); } //放进底部实例 BottomViewItem.getInstance().setUrl(url); BottomViewItem.getInstance().setSelectIcon(selectIcon);
BottomViewItem.getInstance().setUnSelectIcon(unSelectIcon);
//这里序列化的时候,在上面已经被BottomViewItem,和Bitmap引用到了,所以在序列化的时候也会一并把其他都序列化了。
serializeTabbarData(tabbarEntity); tabbarEntity.setAppTabList(tabList);
---------------------------------
然后来总结下。。
下次在序列化对象的时候,一定要在对象构造完后。先序列化,在被其他对象引用里面的值,否者他们就会有关联关系,。。。。。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法