Reputation: 642
I'm using an xml document on resources/raw in order to fill a ListView, here's the code:
List<Item> items = new ArrayList<Item>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.data)));
XmlPullParser p = XmlPullParserFactory.newInstance().newPullParser();
p.setInput(br);
int tipoevento = p.next();
while (tipoevento != XmlPullParser.END_DOCUMENT) {
switch (tipoevento) {
case XmlPullParser.START_TAG:
items.add(
new Item(
Integer.parseInt(p.getAttributeValue(null,"id")),
p.getAttributeValue(null,"name"),
p.getAttributeValue(null,"url")));
break;
default:break;
}
tipoevento=p.next();
}
br.close();
}
catch(Exception e) {
Log.e("Error: ", e.getMessage());}
And, when I run it, I get the following error: Error:﹕ Invalid int: "null".
And the listview is empty
The xml document isn't empty, I did it, here it is:
<?xml version="1.0" encoding="utf-8"?>
<filmList>
<film name="Following" id="R.drawable.following" url="http://www.imdb.com/title/tt0154506/"/>
<film name="Memento" id="R.drawable.memento" url="http://www.imdb.com/title/tt0209144/"/>
<film name="Batman Begins" id="R.drawable.batman_begins" url="http://www.imdb.com/title/tt0372784/"/>
<film name="The Prestige" id="R.drawable.the_prestige" url="http://www.imdb.com/title/tt0482571/"/>
<film name="The Dark Knight" id="R.drawable.the_dark_knight" url="http://www.imdb.com/title/tt0468569/"/>
<film name="Inception" id="R.drawable.inception" url="http://www.imdb.com/title/tt1375666/"/>
<film name="The Dark Knight Rises" id="R.drawable.the_dark_knight_rises" url="http://www.imdb.com/title/tt1345836/"/>
</filmList>
Upvotes: 0
Views: 30
Reputation: 3899
You need to check for a null value
if (p.getAttributeValue(null,"id") != null) {
items.add(
new Item(
Integer.parseInt(p.getAttributeValue(null,"id")),
p.getAttributeValue(null,"name"),
p.getAttributeValue(null,"url")));
}
Upvotes: 1