标签:序列化
项目中遇到 从也个页面向另一个页面跳转传递一个List 封装的对象 。按网上查的资料 需要把 对象 实现 Serializable接口。
写了一下,但是跳转直接崩溃,一直看错误之日找不到原因后来自习教案代码,发现bean里面还有其他bean,对象里的对象没有 进行序列化。记录分享一下。
代码如下:
两个Bean 文件
import java.io.Serializable; import java.util.List; /** * ${JHS} */ public class KnowledgeClassifyChildBean implements Serializable { /** * 知识点ID * */ public String knowledgeId; /** * 知识点名称 */ public String knowledgeName; /** * 知识点题数 */ public int Sum; public boolean isSelect = false; }
import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * ${JHS} */ public class KnowledgeClassifyBean implements Serializable { /* { "knowledgeId": 387,//知识点ID "knowledgeName": "认识立体几何图形",//知识点名称 "Sum":15 //知识点题数 */ private static final long serialVersionUID = 1L; /** * 章节ID * */ public String chapterId; /** * 章节名称 */ public String chapterName; public boolean isSelect = false; <span style="background-color: rgb(255, 0, 0);"> public List<KnowledgeClassifyChildBean> knowledgeList = new ArrayList<KnowledgeClassifyChildBean>();</span> }
KnowledgeClassifyBean mKnowledgeClassifyBean ;
List<KnowledgeClassifyBean> knowledgeClassifyBeanList = new ArrayList<KnowledgeClassifyBean>();
for (int i = 0; i < 6; i++) {
mKnowledgeClassifyBean = new KnowledgeClassifyBean();
mKnowledgeClassifyBean.chapterName = "章节名称 " + i + i;
for (int j = 0; j < 6; j++) {
KnowledgeClassifyChildBean mKnowledgeList = new KnowledgeClassifyChildBean();
mKnowledgeList.knowledgeName = " 名称 " + j;
mKnowledgeClassifyBean.knowledgeList.add(mKnowledgeList);
}
knowledgeClassifyBeanList.add(mKnowledgeClassifyBean);
}
Intent intent = new Intent(A.this, B.class);
if (knowledgeClassifyBeanList != null) {
intent.putExtra("knowledgeClassifyBeanList", (Serializable) knowledgeClassifyBeanList);
startActivity(intent);
}
B这边 Activity 接收
Intent intent = getIntent(); List<KnowledgeClassifyBean> knowledgeClassifyBeanList = ( List<KnowledgeClassifyBean>) intent.getSerializableExtra("knowledgeClassifyBeanList"); Log.i("-----knowledge--" ,knowledgeClassifyBeanList.size() + "");
KnowledgeClassifyBean knowledgeClassifyBean = new KnowledgeClassifyBean();
intent.putExtra("knowledgeClassifyBean ", knowledgeClassifyBean );
接收的Activity
Intent intent = getIntent();
KnowledgeClassifyBean knowledgeClassifyBean =
(KnowledgeClassifyBean
)intent.getSerializableExtra("knowledgeClassifyBean ");
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:序列化
原文地址:http://blog.csdn.net/jia635/article/details/47023777