问题
04-21 20:09:17.590: W/System.err(23059): org.simpleframework.xml.core.PersistenceException: Constructor not matched for class com.example.simplexml.DayData
my main class where I am saving the data in the xml File which seems to work fine
Serializer serializer = new Persister();
TimetableData timetableData = new TimetableData();
SubjectData programming = new SubjectData("Programming","ECG 12",1200, 1400);
SubjectData electronics = new SubjectData("Electronics","ECG 13", 1400, 1600);
timetableData.daysData.get(0).subjectsData.add(electronics);
timetableData.daysData.get(1).subjectsData.add(programming);
File result = new File(this.getFilesDir().getPath().toString() + "example.xml");
try {
serializer.write(timetableData, result);
}
at this moment, if I look at the xmlfile, I can see it, and it looks alright.. but when I read it, it throws the exception. This is how I am reading it.
TimetableData timetableData;
try {
timetableData = serializr.read(TimetableData.class, source);//this throws the exception
DayData myDay= timetableData.daysData.get(0);
SubjectData mySub = myDay.subjectsData.get(0);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
class daydata
public class DayData {
@Attribute(name="dayName")
private String dayName;
@ElementList(name="subjectsData", inline=true)
public List<SubjectData> subjectsData = new ArrayList<SubjectData>();
public DayData(String dayName)
{
this.dayName = dayName;
}
public String getDayName()
{
return dayName;
}
}
my Class TimetableData that makes a list of dayData
@Root
public class TimetableData {
@ElementList(name="daysData",inline=true)
public List<DayData> daysData;
public TimetableData()
{
daysData = new ArrayList<DayData>();
daysData.add(new DayData("monday"));
daysData.add(new DayData("tuesday"));
daysData.add(new DayData("wednesday"));
daysData.add(new DayData("thursday"));
daysData.add(new DayData("friday"));
}
}
my class subjectData
public class SubjectData{
@Element(name="classLoc")
private String classLoc;
@Element(name="startTime")
private int startTime;
@Element(name="endTime")
private int endTime;
@Attribute(name="subjectName")
private String subjectName;
public SubjectData() {
super();
}
public SubjectData(String subjectName, String classLoc, int startTime, int endTime) {
this.classLoc = classLoc;
this.subjectName = subjectName;
this.startTime = startTime;
this.endTime = endTime;
}
public String getClassLoc() {
return classLoc;
}
public int getStartTime()
{
return startTime;
}
public int getEndTime()
{
return endTime;
}
public String getDayName() {
return subjectName;
}
}
This is the xmlFile created
<timetableData>
<dayData dayName="monday">
<subjectData subjectName="Electronics">
<classLoc>ECG 13</classLoc>
<startTime>1400</startTime>
<endTime>1600</endTime>
</subjectData>
</dayData>
<dayData dayName="tuesday">
<subjectData subjectName="Programming">
<classLoc>ECG 12</classLoc>
<startTime>1200</startTime>
<endTime>1400</endTime>
</subjectData>
</dayData>
<dayData dayName="wednesday"/>
<dayData dayName="thursday"/>
<dayData dayName="friday"/>
</timetableData>
回答1:
Try this:
public class DayData {
@Attribute(name="dayName")
private String dayName;
@ElementList(name="subjectsData", inline=true)
public List<SubjectData> subjectsData = new ArrayList<SubjectData>();
public DayData(@Attribute(name="dayName") String dayName)
{
this.dayName = dayName;
}
public String getDayName()
{
return dayName;
}
}
来源:https://stackoverflow.com/questions/16135304/built-a-class-timetabledata-using-simpleframework-that-saves-fine-as-xml-but