尝试在AsyncTask中解析的java.lang.StackOverflowError

时间:2014-02-05 16:28:43

标签: java android parsing android-asynctask

Previous question。在上一个问题中,我询问了如何AndroidSaxParser中的AsyncTask。所以这个问题没有对我有用的好答案,我用了一些东西。正如标题所说我的LogCat抛出一个错误,我不知道如何修复,我已经尝试了很多东西,最后我没有得到任何语法错误。

我的活动代码:

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);
        }
    }
}

这是它抛出的错误:

error

1 个答案:

答案 0 :(得分:0)

引发了SOError,因为DownloadWebPageTask创建了一个新的MessageList,它创建了一个新的DownloadWebPageTask,它创建了一个新的MessageList,用于创建一个新的DownloadWebPageTask,创建一个新的MessageList,创建一个新的DownloadWebPageTask ......等等。