【问题标题】:FATAL EXCEPTION: AsyncTask #1 while implementing RSS Reader致命异常:实现 RSS 阅读器时的 AsyncTask #1
【发布时间】:2014-08-12 10:41:50
【问题描述】:

我正在尝试实现一个 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

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

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

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/

非常感谢任何帮助。

谢谢。

【问题讨论】:

  • 你的其中一个是 Null 请检查 xpp 是否为 null 我假设 xpp.getName()
  • 感谢@ItzikSamara,但代码执行直到该行才到达。当我使用断点进行调试时,代码不会超出该行: xpp.setInput(getInputStream(url), "UTF_8");所以我认为错误就在这一行。我也尝试过使用不同的网址,但效果不佳。
  • 所以它说你的InputStream 是空的。检查getInputStream(URL url)中是否有异常
  • 感谢@AlexanderSukharev 是的,当我调用 getInputStream(URL url) 时,我遇到了 IOException。我尝试传递不同的 url,但没有用。我无法弄清楚问题可能是什么。我也在清单中添加了互联网权限,因此我们可以排除这种情况。
  • 请发布 IOException 的堆栈跟踪。

标签: java android android-asynctask rss


【解决方案1】:

在我看来,这个问题是由连接错误引起的。网站已关闭,连接受到干扰,或者您连接不正确。

我建议使用这个 RSS 库来简化事情: https://github.com/Pkmmte/PkRSS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2015-07-16
    • 2015-07-12
    相关资源
    最近更新 更多