我試圖解析這個XML提要Android的XML解析空指針異常
http://www.billboard.com/rss/charts/hot-100
但每當我試着去閱讀說明標籤,它拋出NullPointerException
。 Theres標籤在文檔的開始是這個標籤導致問題?
我能夠成功拉出所有其他字段。
這裏是我的代碼:
Element element = result.getDocumentElement();
NodeList list = element.getElementsByTagName("item");
try{
if(list != null && list.getLength()>0){
for(int i=0;i<list.getLength();i++){
Element entry = (Element)list.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
String strTitle = title.getFirstChild().getNodeValue();
Log.i("Zealdeveloper", "Title");
Element link = (Element)entry.getElementsByTagName("link").item(0);
String strLink = link.getFirstChild().getNodeValue();
Log.i("Zealdeveloper", "Link");
Element pubdate = (Element)entry.getElementsByTagName("pubDate").item(0);
String strPubdate = pubdate.getFirstChild().getNodeValue();
Log.i("Zealdeveloper", "pubdate");
Element description = (Element)entry.getElementsByTagName("description").item(0);
String strDescription = description.getFirstChild().getNodeValue();
Log.i("Zealdeveloper", "Description"); } }
例外,我越來越:
12-06 13:44:09.202: I/Zealdeveloper(824): Title
12-06 13:44:09.202: I/Zealdeveloper(824): Link
12-06 13:44:09.202: I/Zealdeveloper(824): pubdate
12-06 13:44:09.202: W/System.err(824): java.lang.NullPointerException
12-06 13:44:09.202: W/System.err(824): at com.zealdeveloper.musicfreak.MusicFreakActivity$ParseXmlFeeds.onPostExecute(MusicFreakActivity.java:152)
12-06 13:44:09.212: W/System.err(824): at com.zealdeveloper.musicfreak.MusicFreakActivity$ParseXmlFeeds.onPostExecute(MusicFreakActivity.java:1)
12-06 13:44:09.212: W/System.err(824): at android.os.AsyncTask.finish(AsyncTask.java:417)
12-06 13:44:09.212: W/System.err(824): at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-06 13:44:09.212: W/System.err(824): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-06 13:44:09.212: W/System.err(824): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 13:44:09.212: W/System.err(824): at android.os.Looper.loop(Looper.java:123)
12-06 13:44:09.212: W/System.err(824): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 13:44:09.212: W/System.err(824): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 13:44:09.212: W/System.err(824): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 13:44:09.212: W/System.err(824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 13:44:09.222: W/System.err(824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 13:44:09.222: W/System.err(824): at dalvik.system.NativeStart.main(Native Method)
謝謝!
我沒有回答這個問題。但根據我的經驗,我經常遇到DOM解析器中的空指針異常。然後我轉移到SAX Parser,對我來說工作非常順利。 –
thnx爲提示夥伴,如果你有任何好的來源或教程添加到列表視圖使用saxparser那麼它將是非常有益的.. – ZealDeveloper
你可以試試這個http://iamvijayakumar.blogspot.com/2011/03/android -webservice-with.html –