在android中使用Simple库反序列化一个类对象

时间:2016-05-03 10:40:59

标签: java android xml serialization

因此,我尝试序列化一个对象并将其作为XML写入文件中,这非常有效。因此,当我尝试读取该XML文件并将其反序列化为类对象时,我得到一个空引用异常。

我正在使用这个例子http://www.rhyous.com/2011/10/23/android-and-xml-serialization-with-simple/并且它可以工作,但这只是一个简单的类。

在我的情况下,有两个类,其中一个类中有一个类型为第二类的对象列表。

这是任务类:

@Root (name = "Task")
public class Task {
    @Element
    public String Name;
    @Element
    public int Image;
    @ElementList (type = Item.class)
    public ArrayList<Item> Items;
    @Element
    boolean IsChecked;

    Task(){}
    Task(String name, int image, ArrayList<Item> items, boolean isChecked){
        this.Name = name;
        this.Image = image;
        this.Items = items;
        this.IsChecked = isChecked;
    }
}

这是Item类:

@Root (name = "Item")
public class Item {
    @Element
    public String Name;

    @Element
    public String Image;

    @Element
    public boolean IsChecked;

    @Element
    public String Description;

    Item(String name, String image, String description) {
        this.Name = name;
        this.Image = image;
        this.IsChecked = false;
        this.Description = description;
    }

    Item(String name, String image, boolean isChecked, String description) {
        this.Name = name;
        this.Image = image;
        this.IsChecked = isChecked;
        this.Description = description;
    }
}

这是主要活动:

public class MainActivity extends AppCompatActivity {

    private static final String APP_DIR_PATH = Environment.getExternalStorageDirectory() + File.separator + "MyApp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Item> items = new ArrayList<Item>();
        items.add(new Item("firstItem", APP_DIR_PATH + "/firstPic.jpg", "firstDescription"));
        items.add(new Item("secondItem", APP_DIR_PATH + "/secondPic.jpg", "secondDescription"));
        items.add(new Item("thirdItem", APP_DIR_PATH + "/thirdPic.jpg", "thirdDescription"));

        Task firstTask = new Task("firstTask", 1, items, true);

        // Serialization and writing to file
        File xmlFile = new File(APP_DIR_PATH + "/Task.xml");
        try {
            Serializer serializer = new Persister();
            serializer.write(firstTask, xmlFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Deserialization and reading from file
        Task secondTask = null;
        if (xmlFile.exists()) {
            try {
                Serializer serializer = new Persister();
                secondTask = serializer.read(Task.class, xmlFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // This throws a null reference exception
        Toast.makeText(getBaseContext(), secondTask.Name, Toast.LENGTH_SHORT).show();

        // If I try to serialize the object and write it to file, the file is empty, but that's to be expected :)
        File secondXmlFile = new File(APP_DIR_PATH + "/secondTask.xml");
        try {
            Serializer serializer = new Persister();
            serializer.write(secondTask, secondXmlFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

就在我发布问题时我发现了问题 - 在Item类中应该有一个默认构造函数

Item(){}

在处理序列化/反序列化时,必须使用默认构造函数。