Ror*_*ory 8 java android inner-classes simple-framework
我刚刚开始尝试使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
}
将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>
Run Code Online (Sandbox Code Playgroud)
我是如何注释SimpleXML类或我试图读取它们的问题?谢谢!
Jon*_*nik 12
我在将一些深度嵌套的XML数据读入Java对象时也遇到了这种情况(并希望通过在同一文件中定义类来保持对象结构的简单).
解决方案(不涉及拆分成单独的文件)是使嵌套类静态.(换句话说,将内部类转换为静态嵌套类.)在回顾中有点明显.
例;
嵌套结构:
// ScoreData
// Sport
// Category
// Tournament
Run Code Online (Sandbox Code Playgroud)
Java的:
@Root
public class ScoreData {
@ElementList(entry = "Sport", inline = true)
List<Sport> sport;
static class Sport {
@ElementList(entry = "Category", inline = true)
List<Category> category;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
免责声明:我意识到OP已经解决了问题,但也许这可以帮助其他人遇到
org.simpleframework.xml.core.ConstructorException: Can not construct inner class并且不想像Peter的回答所暗示的那样在单独的文件中定义类.
尝试@Root从allergy课堂上删除.
另外:你在这个单独的文件中是否有这两个类:allergy.java和medalertdata.java?