DeSerializing一个对象时的类强制转换异常

时间:2017-06-06 07:41:48

标签: android serialization nullpointerexception deserialization classcastexception

概述

我是Android开发的新手,我正在尝试序列化然后反序列化对象的值,但我得到例外。

异常

致命例外:主要

java.lang.ClassCastException:java.io.File无法强制转换为com.testingapplication.SchoolNameDetails com.testingapplication.fragment.HomeFragment $ 1.onClick(HomeFragment.java:62)

我尝试了什么

SchoolNameDetails类如下:

public class SchoolNameDetails implements Serializable {
private int id;
private String schoolname;
private String level;
private String pkno;
private String nano;
private String ucname;
private String officeno;
private static final long serialVersionUID = 46548923;

public SchoolNameDetails(){}

public SchoolNameDetails(String schoolname, String level, String pkno,String nano, String ucname, String officeno){
    this.schoolname=schoolname;
    this.level=level;
    this.pkno=pkno;
    this.nano=nano;
    this.ucname=ucname;
    this.officeno=officeno;
}

public SchoolNameDetails(int id, String schoolname, String level, String pkno,String nano, String ucname, String officeno){
    this.id=id;
    this.schoolname=schoolname;
    this.level=level;
    this.pkno=pkno;
    this.nano=nano;
    this.ucname=ucname;
    this.officeno=officeno;
}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getSchoolname() {
    return schoolname;
}
public void setSchoolname(String schoolname) {
    this.schoolname = schoolname;
}
public String getLevel() {
    return level;
}
public void setLevel(String level) {
    this.level = level;
}
public String getPkno() {
    return pkno;
}
public void setPkno(String pkno) {
    this.pkno = pkno;
}
public String getNano() {
    return nano;
}
public void setNano(String nano) {
    this.nano = nano;
}
public String getUcname() {
    return ucname;
}
public void setUcname(String ucname) {
    this.ucname = ucname;
}
public String getOfficeno() {
    return officeno;
}
public void setOfficeno(String officeno) {
    this.officeno = officeno;
}

}

在这里我正在序列化对象

@Override
protected void onStop() {
    super.onStop();
    SchoolNameDetails details = new SchoolNameDetails();
    details.setSchoolname(t2.getText().toString());
    details.setLevel(t2a.getText().toString());
    details.setPkno(t5.getText().toString());
    details.setNano(t6.getText().toString());
    details.setUcname(t7.getText().toString());
    details.setOfficeno(t8.getText().toString());

    saveObject(details);

}

public void saveObject(SchoolNameDetails d) {
    try
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/sdcard/save_object.ser")));
        oos.flush();
        oos.close();
    }
    catch(Exception ex)
    {
        Log.v("Error",ex.getMessage());
        ex.printStackTrace();
    }

}

,我在这里反序列化Fragment类中的对象

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);
    Button b1= (Button) view.findViewById(R.id.button1);
    final TextView t1 = (TextView) view.findViewById(R.id.textView1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SchoolNameDetails details = (SchoolNameDetails)loadSerializedObject(new File("/sdcard/save_object.ser"));
            System.out.println("Name : " + details.getSchoolname());
        }


    });

    return view;
}
public Object loadSerializedObject(File f)
{
    try
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
        Object o = ois.readObject();

    }
    catch(Exception ex)
    {
        Log.v("Error : ","" + ex.getMessage());
        ex.printStackTrace();
    }
    return f;
}

现在最后一个返回值给出异常。我尝试使用return null然后它给出了空指针异常。请帮助我卡住这几个小时并且没有找到任何解决方案

2 个答案:

答案 0 :(得分:0)

首先更改您的saveObject方法以实际编写对象:

public void saveObject(SchoolNameDetails d) {
    try
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/sdcard/save_object.ser")));
        //write to ObjectOutputStream
        oos.writeObject(d);
        oos.flush();
        oos.close();
    }
    catch(Exception ex)
    {
        Log.v("Error",ex.getMessage());
        ex.printStackTrace();
    }
}

接下来更改您的loadSerializedObject方法以从输入流返回对象:

public SchoolNameDetails loadSerializedObject(File f)
{
    try
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
        SchoolNameDetails o = (SchoolNameDetails ) ois.readObject();
        return o;
    }
    catch(Exception ex)
    {
        Log.v("Error : ","" + ex.getMessage());
        ex.printStackTrace();
    }
    return null;
}

最后在你的onClick中确保对象不为空:

    @Override
    public void onClick(View view) {
        SchoolNameDetails details = loadSerializedObject(new File("/sdcard/save_object.ser"));
        if (details != null) {
            System.out.println("Name : " + details.getSchoolname());
        }
    }

答案 1 :(得分:0)

您在功能结束时返回f。但你必须返回o(你从方法ois.readObject()得到的东西。