gpt4 book ai didi

java - 我的 RSS 阅读器给出了 java.lang.NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:24 25 4
gpt4 key购买 nike

我正在尝试创建 RSS 提要阅读器应用程序(按照本教程 http://www.androidbegin.com/tutorial/android-xml-parsing-dom-tutorial/ 进行操作)

并出现以下错误(不幸的是,我的应用程序在显示数据时停止工作)

snapshot of the error

@Override
protected void onPostExecute(Void args) {

for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// Set the texts into TextViews from item nodes
// Get the title
textview.setText(textview.getText() + "Title : "
+ getNode("title", eElement) + "\n" + "\n");
// Get the description
textview.setText(textview.getText() + "Description : "
+ getNode("description", eElement) + "\n" + "\n");
// Get the link
//textview.setText(textview.getText() + "Link : "
// + getNode("source url", eElement) + "\n" + "\n");
// Get the date
textview.setText(textview.getText() + "Date : "
+ getNode("pubDate", eElement) + "\n" + "\n" + "\n"
+ "\n");
}
}
// Close progressbar
pDialog.dismiss();
}

我必须做什么才能使其发挥作用?

这是我的完整代码

  package com.healthyhub.nadeesha.healthyhubz;

import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
// Declare variables
TextView textview;
NodeList nodelist;
ProgressDialog pDialog;
// Insert image URL
String URL = "http://www.livescience.com/home/feed/health.xml";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate a TextView in your activity_main.xml layout
textview = (TextView) findViewById(R.id.text);
// Execute DownloadXML AsyncTask
new DownloadXML().execute(URL);
}

// DownloadXML AsyncTask
private class DownloadXML extends AsyncTask<String, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressbar
pDialog = new ProgressDialog(MainActivity.this);
// Set progressbar title
pDialog.setTitle("Android App");
// Set progressbar message
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
// Show progressbar
pDialog.show();
}

@Override
protected Void doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Download the XML file
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Locate the Tag Name
nodelist = doc.getElementsByTagName("item");

} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;

}
@Override
protected void onPostExecute(Void args) {

for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// Set the texts into TextViews from item nodes
// Get the title
textview.setText(textview.getText() + "Title : "
+ getNode("title", eElement) + "\n" + "\n");
// Get the description
textview.setText(textview.getText() + "Description : "
+ getNode("description", eElement) + "\n" + "\n");
// Get the link
//textview.setText(textview.getText() + "Link : "
// + getNode("source url", eElement) + "\n" + "\n");
// Get the date
textview.setText(textview.getText() + "Date : "
+ getNode("pubDate", eElement) + "\n" + "\n" + "\n"
+ "\n");
}
}
// Close progressbar
pDialog.dismiss();
}
}

// getNode function
private static String getNode(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}

这就是我的 logcat 的样子

enter image description here

最佳答案

您是否已通过真实手机或模拟器逐步使用调试器?通常,您应该添加一个检查以确保节点列表不为空,但除此之外,在代码执行时查看实际值会非常有帮助!

关于java - 我的 RSS 阅读器给出了 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com