问题描述
上一个问题.在我之前的问题中,我询问了如何在 AsyncTask
中使用 AndroidSaxParser
.所以这个问题没有对我有用的好的答案,我曾经尝试过一些事情.正如标题所说,我的 LogCat
抛出一个错误,我不知道如何解决是我尝试了很多东西,但最终没有出现任何语法错误.
Previous question. In my previous question I asked about how to us a AndroidSaxParser
in a AsyncTask
. So that question had no good answer which worked for me and I used tried some things out. As the title says my LogCat
throws an error and I don't know how to fix is I have tried a lot of things and I finally didn't get any syntax errors.
我的活动代码:
package org.developerworks.android;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MessageList extends ListActivity
{
private List<Message> messages;
DownloadWebPageTask task = new DownloadWebPageTask();
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
task.execute();
// loadFeed(ParserType.ANDROID_SAX);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, ParserType.ANDROID_SAX.ordinal(), ParserType.ANDROID_SAX.ordinal(), R.string.android_sax);
menu.add(Menu.NONE, ParserType.SAX.ordinal(), ParserType.SAX.ordinal(), R.string.sax);
menu.add(Menu.NONE, ParserType.DOM.ordinal(), ParserType.DOM.ordinal(), R.string.dom);
menu.add(Menu.NONE, ParserType.XML_PULL.ordinal(), ParserType.XML_PULL.ordinal(), R.string.pull);
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
super.onMenuItemSelected(featureId, item);
ParserType type = ParserType.values()[item.getItemId()];
ArrayAdapter<String> adapter = (ArrayAdapter<String>) this.getListAdapter();
if (adapter != null)
{
if (adapter.getCount() > 0)
{
adapter.clear();
}
}
task.execute();
// this.loadFeed(type);
return true;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent viewMessage = new Intent(Intent.ACTION_VIEW, Uri.parse(messages.get(position).getLink().toExternalForm()));
this.startActivity(viewMessage);
}
private void loadFeed(ParserType type)
{
try
{
Log.i("AndroidNews", "ParserType=" + type.name());
FeedParser parser = FeedParserFactory.getParser(type);
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages)
{
titles.add(msg.getTitle());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row, titles);
this.setListAdapter(adapter);
}
catch (Throwable t)
{
Log.e("AndroidNews", t.getMessage(), t);
}
}
private class DownloadWebPageTask extends AsyncTask<List<String>, Void, List<String>>
{
MessageList msgl = new MessageList();
@Override
protected List<String> doInBackground(List<String>... urls)
{
FeedParser parser = FeedParserFactory.getParser(ParserType.ANDROID_SAX);
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages)
{
titles.add(msg.getTitle());
}
return titles;
}
@Override
protected void onPostExecute(List<String> result)
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(msgl, R.layout.row, result);
msgl.setListAdapter(adapter);
}
}
}
这是它抛出的错误:
推荐答案
SOError 被抛出,因为 DownloadWebPageTask 创建了一个新的 MessageList,它创建了一个新的 DownloadWebPageTask,它创建了一个新的 MessageList,它创建了一个新的 DownloadWebPageTaskcreate一个新的 MessageList,它创建了一个新的 DownloadWebPageTask...等等.
The SOError is thrown because DownloadWebPageTask creates a new MessageList that creates a new DownloadWebPageTask that creates a new MessageList that creates a new DownloadWebPageTaskcreates a new MessageList that creates a new DownloadWebPageTask... and so on.
这篇关于试图在 AsyncTask 中解析的 java.lang.StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!