1
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;
}
}

dayData のリストを作成するクラス TimetableData

    @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>
4

1 に答える 1

1

これを試して:

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;
}
}
于 2013-04-22T09:04:55.160 に答える