私のアプリはニュースWebページからタイトルをダウンロードし、リストビュー(RSSReader)に表示します。実行しようとすると、エラーが表示されます。
コード:-
public class RSSReaderActivity extends ListActivity
{ List headlines, links;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DoSomeTask().execute();
}
// DoSomeTask class will do all the work on another thread so there will be no
// ANR and UI hanging.
private class DoSomeTask extends AsyncTask<Void, Void, Void>
{
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Void doInBackground(Void... arg0) {
try {
headlines = new ArrayList();
links = new ArrayList();
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");
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();
}
return null;
}
protected void onPostExecute(Void params)
{
try{
// Binding data
ArrayAdapter adapter = new ArrayAdapter(RSSReaderActivity.this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}catch (Exception e) {}
}
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse((String) links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
Logcat:-
06-23 19:37:57.505: W/dalvikvm(7450): threadid=8: thread exiting with uncaught exception (group=0x400207d8)
06-23 19:37:57.785: E/AndroidRuntime(7450): FATAL EXCEPTION: AsyncTask #1
06-23 19:37:57.785: E/AndroidRuntime(7450): java.lang.RuntimeException: An error occured while executing doInBackground()
06-23 19:37:57.785: E/AndroidRuntime(7450): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.lang.Thread.run(Thread.java:1096)
06-23 19:37:57.785: E/AndroidRuntime(7450): Caused by: java.lang.IllegalArgumentException
06-23 19:37:57.785: E/AndroidRuntime(7450): at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1040)
06-23 19:37:57.785: E/AndroidRuntime(7450): at mohit.apps.rssreaderfast.RSSReaderActivity$DoSomeTask.doInBackground(RSSReaderActivity.java:56)
06-23 19:37:57.785: E/AndroidRuntime(7450): at mohit.apps.rssreaderfast.RSSReaderActivity$DoSomeTask.doInBackground(RSSReaderActivity.java:1)
06-23 19:37:57.785: E/AndroidRuntime(7450): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-23 19:37:57.785: E/AndroidRuntime(7450): ... 4 more
そうですね、エラーはdoInBackground()にあります。私は新しいマルチスレッドプログラミングなので、コードの問題点を指摘してください。ありがとう。