2013-06-02 13 views
7

sto usando XmlPullParser su Android ma ottengo getText return null. Perché sta succedendo questo?Xml getText return null - Android

Il codice, la linea ha commentato dà il nulla

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"); 
    } 
+0

xpp.getName() restituisce null o esiste una eccezione NullPointerException quando viene eseguita tale riga? – Ryan

+0

Ho cambiato la riga per getText, era sbagliato prima .. Una NullPointerException quando viene eseguita – Clepto

risposta

8

Prova questo:

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

Inoltre, assicurarsi che l'itemsList è inizializzato.

+1

Grazie! Ha funzionato! – Clepto

+0

Ho lo stesso qui nella riga 5, http://pastebin.com/2F5CqxRK Perché? – Clepto

+0

Quale parte restituisce nulla? getName() o getText()? Usa anche && nelle tue dichiarazioni condizionali. – Ryan