7

我在 Android 上使用 XmlPullParser,但 getText 返回 null。为什么会这样?

代码,注释行给出了 null

    ArrayList<String> titleList = new ArrayList<String>();
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput(this.getInputStream(), null);
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equalsIgnoreCase(TITLE)) {
//                  MainActivity.itemsList.add(xpp.getText());
                    Log.d("XGamers", "a");
                }
            }``
            eventType = xpp.next();
        }
    } catch (XmlPullParserException e) {
        Log.e("XGamers", "XmlPullParserException in FeedParser");
    } catch (IOException e) {
        Log.e("XGamers", "IOException in FeedParser");
    }
4

1 回答 1

10

试试这个:

if (xpp.getName().equalsIgnoreCase(TITLE)) {
  if(xpp.next() == XmlPullParser.TEXT) { 
       MainActivity.itemsList.add(xpp.getText());
       Log.d("XGamers", "a");
  }
}

此外,请确保您的 itemsList 已初始化。

于 2013-06-02T09:55:49.323 回答