问题
I'm just beginning to experiment with Android Development with SimpleXML and thought it was going quite well until I hit a snag. The code below produces an exception of
W/System.err(665): org.simpleframework.xml.core.ConstructorException: Can not construct inner class
I've looked through the questions on inner classes and think I understand why you would use them (not that mine was necessarily intentional) but despite moving my code round to try and avoid usage I'm still a little stuck and would appreciate any help.
Source Code:
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;
}
}
}
With the XML file referenced structured as:
<?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>
Is the problem with how I have annotated the SimpleXML classes or where I am trying to read them? Thanks!
回答1:
Try removing @Root
from the allergy
class.
Also: do you have this two classes each in it's separate file: allergy.java and medalertdata.java?
回答2:
I ran into this too while reading some deeply nested XML data into Java objects (and wanting to keep the object structure simple by defining the classes in the same file).
The solution (that doesn't involve splitting into separate files) was to make the nested classes static. (In other words, convert inner classes into static nested classes.) Kinda obvious in retrospective.
Example;
Nested structure:
// ScoreData
// Sport
// Category
// Tournament
Java:
@Root
public class ScoreData {
@ElementList(entry = "Sport", inline = true)
List<Sport> sport;
static class Sport {
@ElementList(entry = "Category", inline = true)
List<Category> category;
}
// ...
}
Disclaimer: I realise OP got the problem solved already, but maybe this helps others who run into
org.simpleframework.xml.core.ConstructorException: Can not construct inner class
and don't want to define the classes in separate files as Peter's answer suggests.
来源:https://stackoverflow.com/questions/8459057/simplexml-constructor-exception-can-not-create-inner-class