このチュートリアルに従いましたが、アプリケーションを実行しようとすると、「残念ながら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でコンパイルできますが、エミュレーターで実行できませんでした。私が間違っているのは何ですか?