java.lang.StackOverflowError trying to parse in a

2019-01-29 10:49发布

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.

My activity code :

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

and this is the error it throws :

error

1条回答
成全新的幸福
2楼-- · 2019-01-29 10:57

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.

查看更多
登录 后发表回答