Android的ListView控件 - RSSPro应用程序已停止工作? [英] Android ListView - RSSPro app has stopped working?

查看:193
本文介绍了Android的ListView控件 - RSSPro应用程序已停止工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照<一href=\"http://androidresearch.word$p$pss.com/2012/01/21/creating-a-simple-rss-application-in-android/#comments\"相对=nofollow>本教程但是当我尝试运行应用程序我碰到一个不幸的是RSSPro已经停止了。

I've followed this tutorial but when I try to run the application I get "Unfortunately RSSPro has stopped."

这是我的code为RSSProActivity:

This is my code for the RSSProActivity:

package com.android.rss;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RSSProActivity extends ListActivity {

    ArrayList<String> headlines;
    ArrayList<String> links;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


     // Initializing instance variables
        headlines = new ArrayList<String>();
        links = new ArrayList<String>();

        try {
            URL url = new URL("http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml");

            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 adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, headlines);
        setListAdapter(adapter);

    }

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

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String positions = (String) links.get(position);
       Uri uri = Uri.parse(positions);
       Intent intent = new Intent(Intent.ACTION_VIEW, uri);
       startActivity(intent);
    }

}

我main.xml中看起来是这样的:

My main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

LogCat中的消息:

LogCat messages:

05-21 19:44:40.528: D/dalvikvm(528): Not late-enabling CheckJNI (already on)
05-21 19:44:41.716: I/dalvikvm(528): threadid=3: reacting to signal 3
05-21 19:44:41.726: I/dalvikvm(528): Wrote stack traces to '/data/anr/traces.txt'
05-21 19:44:41.946: D/dalvikvm(528): GC_FOR_ALLOC freed 87K, 3% free 9136K/9347K, paused 113ms
05-21 19:44:41.956: I/dalvikvm-heap(528): Grow heap (frag case) to 9.904MB for 960016-byte allocation
05-21 19:44:42.056: D/dalvikvm(528): GC_CONCURRENT freed 1K, 3% free 10072K/10311K, paused 6ms+14ms
05-21 19:44:42.176: D/dalvikvm(528): GC_FOR_ALLOC freed 0K, 3% free 10072K/10311K, paused 38ms
05-21 19:44:42.196: I/dalvikvm-heap(528): Grow heap (frag case) to 11.963MB for 2160016-byte allocation
05-21 19:44:42.217: I/dalvikvm(528): threadid=3: reacting to signal 3
05-21 19:44:42.306: I/dalvikvm(528): Wrote stack traces to '/data/anr/traces.txt'
05-21 19:44:42.316: D/dalvikvm(528): GC_CONCURRENT freed 0K, 2% free 12181K/12423K, paused 3ms+4ms
05-21 19:44:42.526: D/gralloc_goldfish(528): Emulator without GPU emulation detected.
05-21 19:44:48.076: I/dalvikvm(528): threadid=3: reacting to signal 3
05-21 19:44:48.206: D/AndroidRuntime(528): Shutting down VM
05-21 19:44:48.206: W/dalvikvm(528): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-21 19:44:48.216: I/dalvikvm(528): Wrote stack traces to '/data/anr/traces.txt'
05-21 19:44:48.396: E/AndroidRuntime(528): FATAL EXCEPTION: main
05-21 19:44:48.396: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.rss/com.android.rss.RSSProActivity}: android.os.NetworkOnMainThreadException
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.os.Looper.loop(Looper.java:137)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-21 19:44:48.396: E/AndroidRuntime(528):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 19:44:48.396: E/AndroidRuntime(528):  at java.lang.reflect.Method.invoke(Method.java:511)
05-21 19:44:48.396: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-21 19:44:48.396: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-21 19:44:48.396: E/AndroidRuntime(528):  at dalvik.system.NativeStart.main(Native Method)
05-21 19:44:48.396: E/AndroidRuntime(528): Caused by: android.os.NetworkOnMainThreadException
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-21 19:44:48.396: E/AndroidRuntime(528):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-21 19:44:48.396: E/AndroidRuntime(528):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-21 19:44:48.396: E/AndroidRuntime(528):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-21 19:44:48.396: E/AndroidRuntime(528):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-21 19:44:48.396: E/AndroidRuntime(528):  at com.android.rss.RSSProActivity.getInputStream(RSSProActivity.java:92)
05-21 19:44:48.396: E/AndroidRuntime(528):  at com.android.rss.RSSProActivity.onCreate(RSSProActivity.java:48)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.Activity.performCreate(Activity.java:4465)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-21 19:44:48.396: E/AndroidRuntime(528):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-21 19:44:48.396: E/AndroidRuntime(528):  ... 11 more
05-21 19:44:48.616: I/dalvikvm(528): threadid=3: reacting to signal 3
05-21 19:44:48.708: I/dalvikvm(528): Wrote stack traces to '/data/anr/traces.txt'
05-21 19:44:49.116: I/dalvikvm(528): threadid=3: reacting to signal 3
05-21 19:44:49.206: I/dalvikvm(528): Wrote stack traces to '/data/anr/traces.txt'
05-21 19:44:49.436: I/dalvikvm(528): threadid=3: reacting to signal 3
05-21 19:44:49.476: I/dalvikvm(528): Wrote stack traces to '/data/anr/traces.txt'
05-21 19:44:56.997: I/Process(528): Sending signal. PID: 528 SIG: 9

我目前使用SDK版本15(Android版4.0.3)。我的codeS可以在Eclipse编译,但不能在模拟器上运行。它是什么,我做错了?

I'm currently using the SDK 15 version (Android 4.0.3). My codes can be compiled in Eclipse but couldn't run on the emulator. What is it that I'm doing wrong?

推荐答案

您所面临的的 NetworkOnMainThreadException 因为在检索主线程中的XML。使用 的AsyncTask 在后台执行XML的获取和分析。 阅读RSS:样本中可以找到在后台,使用AsyncTask的

这篇关于Android的ListView控件 - RSSPro应用程序已停止工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆