04-21 20:09:17.590: W/System.err(23059): org.simpleframework.xml.core.PersistenceException: Constructor not matched for class com.example.simplexml.DayData
我在我的主类中将数据保存在xml文件中,这似乎很好
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);
}
此时,如果我查看xmlfile,我可以看到它,而且看起来还不错..但是当我阅读它时,它会引发异常。这就是我的阅读方式。
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();
}
上课日数据
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;
}
}
我的Class TimetableData,它列出了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"));
}
}
我班的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;
}
}
这是创建的xmlFile
<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>
最佳答案
尝试这个:
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;
}
}