致命异常:实现RSS阅读器时AsyncTask#1

时间:2014-08-12 10:41:51

标签: java android android-asynctask rss

我正在尝试实现RSS阅读器,但我不断收到RuntimeException。

这是我的.java文件的代码:

public class TopRatedFragment extends Fragment {

Context context;
ListView listView;
List<String> headlines;
List<String> links;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {



    View rootView = inflater.inflate(R.layout.fragment_top_rated,
            container, false);

    context = getActivity().getApplicationContext();
    listView = (ListView) rootView.findViewById(R.id.list);

    new RssFeed1().execute();

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int pos,
                long id) {
            Log.d("ListView", String.valueOf(pos));
            Uri uri = Uri.parse((String) links.get(pos));
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

    return rootView;
}

private class RssFeed1 extends AsyncTask<Void, Void, List<String>> {

    @Override
    protected List<String> doInBackground(Void... params) {
        // Initializing instance variables
        headlines = new ArrayList<String>();
        links = new ArrayList<String>();

        try {
            URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();

                // We will get the XML from an input stream
            xpp.setInput(getInputStream(url), "UTF_8");

                /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
                 * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
                 * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
                 * so we should skip the "<title>" tag which is a child of "<channel>" tag,
                 * and take in consideration only "<title>" tag which is a child of "<item>"
                 *
                 * In order to achieve this, we will make use of a boolean variable.
                 */
            boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {

                    if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;
                    } else if (xpp.getName().equalsIgnoreCase("title")) {
                        if (insideItem)
                            headlines.add(xpp.nextText()); //extract the headline
                    } else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem)
                            links.add(xpp.nextText()); //extract the link of article
                    }
                }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                    insideItem=false;
                }

                eventType = xpp.next(); //move to next element
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Binding data
        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        // R.layout.fragment_top_rated, headlines);

        return headlines;

        // Populate list view with ArrayAdapter
        // ListView listView = (ListView) findViewById(R.id.list);
        // listView.setAdapter(adapter);

    }

    @Override
    protected void onPostExecute(List<String> hList) {

        if (hList == null) {
            Log.v("MyTabNavigation", "hList is null");
        } else {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    context, R.layout.top_rated_textview, hList);

            listView.setAdapter(adapter);
        }
    }

    public InputStream getInputStream(URL url) {
        try {
            return url.openConnection().getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

}
}

这是我的堆栈跟踪:

08-12 06:20:54.470: E/AndroidRuntime(2925): FATAL EXCEPTION: AsyncTask #1
08-12 06:20:54.470: E/AndroidRuntime(2925): java.lang.RuntimeException: An error     occured while executing doInBackground()
08-12 06:20:54.470: E/AndroidRuntime(2925):     at     android.os.AsyncTask$3.done(AsyncTask.java:299)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.lang.Thread.run(Thread.java:841)
08-12 06:20:54.470: E/AndroidRuntime(2925): Caused by: java.lang.IllegalArgumentException: is == null
08-12 06:20:54.470: E/AndroidRuntime(2925):     at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1615)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at com.idsil.rssfeedsample.TopRatedFragment$RssFeed1.doInBackground(TopRatedFragment.java:78)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at com.idsil.rssfeedsample.TopRatedFragment$RssFeed1.doInBackground(TopRatedFragment.java:1)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-12 06:20:54.470: E/AndroidRuntime(2925):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-12 06:20:54.470: E/AndroidRuntime(2925):     ... 4 more

我使用了两个在网上找到的教程。 以下是链接:

这是使用ActionBar教程的UI链接:

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

这是RSS阅读器:

http://androidresearch.wordpress.com/2012/01/21/creating-a-simple-rss-application-in-android/

请非常感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

在我看来,这个问题是由连接错误引起的。网站已关闭,某些内容干扰了连接,或者您没有正确连接。

我建议使用此RSS库来简化操作: https://github.com/Pkmmte/PkRSS