我的问题是我正在使用 Dom Parsing 来解析下面的 xml 文件,但这给了我 NullPointerException 的错误。
任何帮助,将不胜感激。
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView mScrView1 = new ScrollView(this);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView id[];
TextView published[];
TextView content[];
TextView title[];
TextView mediacontent[];
TextView mediathumbnail[];
try {
URL url = new URL(
"http://gdata.youtube.com/feeds/api/users/estudiosabiertostv/uploads");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("entry");
/** Assign textview array length by arraylist size */
id = new TextView[nodeList.getLength()];
published = new TextView[nodeList.getLength()];
content = new TextView[nodeList.getLength()];
title = new TextView[nodeList.getLength()];
mediacontent = new TextView[nodeList.getLength()];
mediathumbnail = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
id[i] = new TextView(this);
published[i] = new TextView(this);
content[i] = new TextView(this);
title[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList idList = fstElmnt.getElementsByTagName("id");
Element idElement = (Element) idList.item(0);
idList = idElement.getChildNodes();
id[i].setText("Id is = "
+ ((Node) idList.item(0)).getNodeValue());
NodeList publishedList = fstElmnt
.getElementsByTagName("published");
Element publishedElement = (Element) publishedList.item(0);
publishedList = publishedElement.getChildNodes();
published[i].setText("published is = "
+ ((Node) publishedList.item(0)).getNodeValue());
NodeList contentList = fstElmnt.getElementsByTagName("content");
Element contentElement = (Element) contentList.item(0);
contentList = contentElement.getChildNodes();
content[i].setText("content is = "
+ ((Node) contentList.item(0)).getNodeValue());
NodeList titleList = fstElmnt.getElementsByTagName("title");
Element titleElement = (Element) titleList.item(0);
titleList = titleElement.getChildNodes();
title[i].setText("title is = "
+ ((Node) titleList.item(0)).getNodeValue());
NodeList nodeList1 = fstElmnt.getElementsByTagName("media:group");
System.out.println("Size is:- " +nodeList1.getLength());
for (int j = 0; j < nodeList1.getLength(); j++) {
Node node1 = nodeList1.item(j);
mediacontent[j] = new TextView(this);
mediathumbnail[j] = new TextView(this);
Element secondElmnt = (Element) node1;
NodeList mediacontentList = secondElmnt.getElementsByTagName("media:content");
Element mediacontentElement = (Element) mediacontentList.item(0);
mediacontentList = mediacontentElement.getChildNodes();
mediacontent[j].setText("mediacontent is = "
+ ((Node) mediacontentList.item(0)).getNodeValue());
layout.addView(mediacontent[j]);
}
layout.addView(id[i]);
layout.addView(published[i]);
layout.addView(content[i]);
layout.addView(title[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
mScrView1.addView(layout);
setContentView(mScrView1);
}
}