2013-06-02 35 views
7

Tôi đang sử dụng XmlPullParser trên Android nhưng nhận được trả về getText null. Tại sao điều này xảy ra?Xml getText return null - Android

Mã, dòng bình luận cho 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"); 
    } 
+0

Có xpp.getName() cho null hoặc có một NullPointerException khi dòng đó được thực thi không? – Ryan

+0

Tôi thay đổi dòng để getText, nó đã sai trước khi .. Một NullPointerException khi được thực hiện – Clepto

Trả lời

8

Hãy thử điều này:

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

Ngoài ra, hãy chắc chắn itemsList của bạn được khởi tạo.

+1

Cảm ơn! Điều đó đã hiệu quả! – Clepto

+0

Tôi nhận được cùng ở đây trong dòng 5, http://pastebin.com/2F5CqxRK Tại sao? – Clepto

+0

Phần nào của nó trả về null? getName() hoặc getText()? Cũng sử dụng && trong các câu lệnh có điều kiện của bạn. – Ryan