2012-05-21 44 views
1

我跟着this tutorial,但是當我嘗試運行該應用程序時,我收到了「不幸的RSSPro已停止。」Android ListView - RSSPro應用程序已停止工作?

這是我的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中看起來是這樣的:

<?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的消息:

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)。我的代碼可以在Eclipse中編譯,但無法在模擬器上運行。我做錯了什麼?

+0

可以在這裏發佈ur logcat文件... – Thamilvanan

+0

添加logCat文件! –

+0

在你的Main.xml文件中使用列表視圖,但在你的代碼中,你不需要任何參考列表視圖爲列表視圖,並設置Ur適配器到列表視圖 – Thamilvanan

回答

1

由於在主線程中檢索到XML,因此您正面臨着NetworkOnMainThreadException。使用AsyncTask在後臺執行XML的讀取和解析。可以在以下位置找到示例:Read RSS in background, using AsyncTask

+0

雅這似乎是問題,我把一個try catch塊現在它可以運行。這很奇怪,因爲本教程的作者可以讓它運行。感謝您弄明白這一點! –

+0

本教程是在發佈Honeycomb SDK之前編寫的。從'NetworkOnMainThreadException'文檔中可以看到,針對Honeycomb之前的SDK版本的應用程序允許在主線程上執行網絡,但如果訪問時間比預期的響應時間長,ANR錯誤將會終止。 – Rajesh

+0

我明白了。非常感謝您的回答! :) –