我刚刚开始尝试使用 SimpleXML 进行 Android 开发,并认为它进展顺利,直到遇到障碍。下面的代码产生一个异常
W/System.err(665):org.simpleframework.xml.core.ConstructorException:无法构造内部类
我已经查看了有关内部类的问题,并认为我理解您为什么要使用它们(不是我的必然是故意的),但是尽管移动了我的代码以尝试避免使用,但我仍然有点卡住,希望有任何帮助.
源代码:
public class InCaseOfEmergencyMedAlertAllergiesActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Serializer serializer = new Persister();
InputStream xmlstream = this.getResources().openRawResource(R.raw.sample_data_allergies);
try {
medalertdata allergyObject = serializer.read(medalertdata.class, xmlstream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.allergies);
}
@Root
public class medalertdata {
@ElementList
private List<allergy> allergyList;
public List getAllergies() {
return allergyList;
}
}
@Root
public class allergy{
@Element
private String to;
@Element
private Boolean medical;
@Element
private String notes;
public allergy(String to, Boolean medical, String notes){
this.to = to;
this.medical = medical;
this.notes = notes;
}
public String getTo() {
return to;
}
public Boolean getMedical() {
return medical;
}
public String getNotes() {
return notes;
}
}
}
引用的 XML 文件结构如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<medalertdata>
<allergy>
<to>Penicillin</to>
<medical>true</medical>
<notes></notes>
</allergy>
<allergy>
<to>Bee Stings</to>
<medical>false</medical>
<notes>Sample</notes>
</allergy>
</medalertdata>
是我如何注释 SimpleXML 类或我试图阅读它们的问题吗?谢谢!