我剛剛開始嘗試使用SimpleXML進行Android開發,並認爲它運行良好,直到遇到障礙。下面的代碼產生SimpleXML構造函數異常 - 無法創建內部類
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的班或者我想這個問題閱讀他們?謝謝!
謝謝你的回覆。不幸的是,刪除@Root符號沒有效果。目前這兩個類都是在onCreate()關閉後在Activity類中聲明的 - 它們是否應該放在不同的文件中? – Rory
是的,Java中的所有普通公共類必須在其自己的文件中聲明。另一個選擇是讓它們成爲'靜態'(不知道SimpleXML如何處理這個)。 –
讓他們在同一個文件中肯定是這個問題=) – Rory