问题:XML解析返回空指针异常,无法从xml中的程序标签中检索到文本。单步执行代码显示:(这是来自调试语句,如下面的代码所示)
err StartTag entry
err StartTag record
err Text 4
调用 parser 的行(也是导致 nullpointer 的行,pail 是一个新创建的容器):
pail = XmlParsee.parsee(getResources().openRawResource(R.raw.testprogramlist));
我的 XML 文件:
<entry>
<record>
<program>Program 1(English)</program>
</record>
<record>
<program>Program 2(Mandarin)</program>
</record>
</entry>
我的解析器(我尝试了 getText() 和 nextText(),都返回相同的问题):
public class XmlParsee {
// jealousy has invaded! Generic xml parser that returns a container
public static Container parsee(InputStream inputriver) {
Container container = null;
try {
// get new parser object from factory
XmlPullParser parsee = XmlPullParserFactory.newInstance().newPullParser();
parsee.setInput(inputriver, null);
int eventType = parsee.getEventType();
// while xml still has more, we read.
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT: {
//these comments to be replaced with logging functions, if you desire
//wokay, we begin
container = new Container();
System.err.println("doc start");
break;
}
case XmlPullParser.END_DOCUMENT:{
//wokay, we end.
break;
}
case XmlPullParser.START_TAG:{
//new tag! first we get the tag's name
String tag = parsee.getName();
System.err.println("StartTag "+parsee.getName());
//then we check, individually, what the tag is to confirm content
//if <program>
if(tag.equalsIgnoreCase(Container.PROGRAM)){
System.err.println("Text "+parsee.TEXT);
//container.addProgramList(parsee.getText());
}
//if <>
break;
}
}
//done with this line, next!
eventType = parsee.next();
}
} catch (Exception e) {
container = null;
}
return container;
}
有任何想法吗?从早上开始我就一直在敲我的头=\