1

我正在尝试构建一个 android RSS 阅读器应用程序,但我不断收到 Null 指针异常。我是 java 新手,不知道它的提要 url 是没有返回任何数据还是数据没有正确存储。

我的 logcat 内容如下:

08-08 18:44:25.840: E/Trace(621): error opening trace file: No such file or directory (2)
08-08 18:44:25.910: W/ActivityThread(621): Application com.app.fullmetalmanga is waiting for the debugger on port 8100...
08-08 18:44:25.930: I/System.out(621): Sending WAIT chunk
08-08 18:44:25.951: I/dalvikvm(621): Debugger is active08-08 18:44:26.140: I/System.out(621): Debugger has connected
08-08 18:44:27.710: I/System.out(621): debugger has settled (1382)
08-08 18:44:29.291: D/gralloc_goldfish(621): Emulator without GPU emulation detected.
08-08 18:44:48.060: W/System.err(621): java.lang.NullPointerException
08-08 18:44:48.760: W/System.err(621):  at java.util.Calendar.setTime(Calendar.java:1324)
08-08 18:44:48.810: W/System.err(621):  at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:536)
08-08 18:44:49.050: W/System.err(621):  at java.text.SimpleDateFormat.format(SimpleDateFormat.java:821)
08-08 18:44:49.150: W/System.err(621):  at java.text.DateFormat.format(DateFormat.java:376)
08-08 18:44:49.490: W/System.err(621):  at com.app.fullmetalmanga.RssItem.getDate(RssItem.java:48)
08-08 18:44:49.810: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$RSSHandler.endElement(MainActivity.java:146)
08-08 18:44:50.090: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:156)
08-08 18:44:50.250: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
08-08 18:44:50.410: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
08-08 18:44:50.610: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
08-08 18:44:50.791: W/System.err(621):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
08-08 18:44:50.990: W/System.err(621):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
08-08 18:44:51.800: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$SAXHelper.parseContent(MainActivity.java:112)
08-08 18:44:51.870: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$loadingTask.doInBackground(MainActivity.java:74)
08-08 18:44:52.350: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$loadingTask.doInBackground(MainActivity.java:1)
08-08 18:44:52.700: W/System.err(621):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-08 18:44:53.070: W/System.err(621):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-08 18:44:53.190: W/System.err(621):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-08 18:44:53.370: W/System.err(621):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-08 18:44:53.561: W/System.err(621):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-08 18:44:53.980: W/System.err(621):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-08 18:44:54.051: W/System.err(621):  at java.lang.Thread.run(Thread.java:856)

我的主要活动:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
ListView lv1;
ProgressDialog ShowProgress;
public ArrayList<RssItem> RssItemList = new ArrayList<RssItem>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv1 = (ListView) findViewById(R.id.listView1);

    ShowProgress = ProgressDialog.show(MainActivity.this, "",
            "Loading. Please wait...", true);
    new loadingTask().execute("http://fandom.com/rss/news/manga");

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri
                    .parse(RssItemList.get(position).getTitle()));
            startActivity(intent);

        }
    });       
}


class loadingTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {

        SAXHelper sh = null;
        try {
            sh = new SAXHelper(urls[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        sh.parseContent("");
        return "";

    }

    protected void onPostExecute(String s) {
        lv1.setAdapter(new EfficientAdapter(MainActivity.this, RssItemList));
        ShowProgress.dismiss();

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




class SAXHelper {
public HashMap<String, String> userList = new HashMap<String, String>();
private URL url2;

public SAXHelper(String url1) throws MalformedURLException {
    this.url2 = new URL(url1);
}

public RSSHandler parseContent(String parseContent) {
    RSSHandler df = new RSSHandler();
    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(df);
        xr.parse(new InputSource(url2.openStream()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return df;
}
}


class RSSHandler extends DefaultHandler {

private RssItem currentPost = new RssItem();
StringBuffer chars = new StringBuffer();

@Override
public void startElement(String uri, String localName, String qName,
        Attributes atts) {

    chars = new StringBuffer();
    if (localName.equalsIgnoreCase("item")) {

    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    if (localName.equalsIgnoreCase("title")
            && currentPost.getTitle() == null) {
        currentPost.setTitle(chars.toString());

    }
    if (localName.equalsIgnoreCase("pubDate")
            && currentPost.getDate() == null) {
        currentPost.setDate(chars.toString());

    }
    /*if (localName.equalsIgnoreCase("thumbnail")
            && currentPost.getThumbnail() == null) {
        currentPost.setThumbnail(chars.toString());

    } */
    if (localName.equalsIgnoreCase("link")
            && currentPost.getLink() == null) {
        currentPost.setLink(chars.toString());
    }

    if (localName.equalsIgnoreCase("item")) {
        RssItemList.add(currentPost);
        currentPost = new RssItem();
    }

}

@Override
public void characters(char ch[], int start, int length) {
    chars.append(new String(ch, start, length));
}

   }

}

我的 RssItem 类:

public class RssItem {

static SimpleDateFormat FORMATTER = 
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
    private String title;
    private URL link;
    private String description;
    private Date date;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title.trim();
    }
    // getters and setters omitted for brevity 
    public URL getLink() {
        return link;
    }

    public void setLink(String link) {
        try {
            this.link = new URL(link);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description.trim();
    }

    public String getDate() {
        return FORMATTER.format(this.date);
    }

    public void setDate(String date) {
        // pad the date if necessary
        while (!date.endsWith("00")){
            date += "0";
        }
        try {
            this.date = FORMATTER.parse(date.trim());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


}

我的代码是使用这个例子编写的。仅更改 URL 并使用我自己修改过的“发布”文件。(重命名为 RssItem)

4

2 回答 2

2

看来,date在 RssItem 中,当您将其传递给您的 SimpleDateFormat 时,会引发异常:

public String getDate() {
    return FORMATTER.format(this.date); // FORMATTER cannot format a null value
}

您需要定义一个默认的date或其他礼貌的方法来处理这个:

public String getDate() {
    if(this.date == null)
        return "";
    return FORMATTER.format(this.date);
}
于 2012-08-09T00:03:12.720 回答
0

看起来您从未在 RssItem 中初始化您的日期属性。如果尚未完成,您可能需要在 getDate() 中添加检查以初始化日期属性。或者更好的是,在您的构造函数中执行此操作。不过,我对使用 RSS 提要不是很熟悉。

于 2012-08-09T00:04:23.560 回答