我正在使用XmlPullParser示例,但是当在API 8或10中运行时,值返回null(我不确定),但在其他版本的API 16 +中工作正常。可能会发生什么?返回null的参数是getAttributeValue(null,“name”)。
更新 进入AndroidManifest使用android:minSdkVersion =“8”
public class SitesXmlPullParser {
static final String KEY_SITE = "site";
public static List<StackSite> getStackSitesFromFile(Context ctx) {
List<StackSite> stackSites;
stackSites = new ArrayList<StackSite>();
StackSite curStackSite = null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
FileInputStream fis = ctx.openFileInput("StackSites.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
xpp.setInput(reader);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xpp.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)) {
curStackSite = new StackSite();
}
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)) {
// This values return null
String name = xpp.getAttributeValue(null, "name");
String link = xpp.getAttributeValue(null, "link");
String about = xpp.getAttributeValue(null, "about");
String image = xpp.getAttributeValue(null, "image");
curStackSite.setName(name);
curStackSite.setLink(link);
curStackSite.setAbout(about);
curStackSite.setImgUrl(image);
stackSites.add(curStackSite);
}
break;
default:
break;
}
eventType = xpp.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return stackSites;
}
}