本文介绍了java.lang.StackOverflowError试图在AsyncTask的解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

previous问题。在我的previous问题,我问怎么给我们一个 AndroidSaxParser 的AsyncTask 。所以,这个问题有哪些工作对我来说没有很好的答案,我曾经尝试了一些东西出来。正如标题所说我的 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.

我的活动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 :

推荐答案

该SOError被抛出,因为DownloadWebPageTask创建一个新的MessageList中,创建一个新的DownloadWebPageTask,创建一个新的MessageList中,创建一个新的DownloadWebPageTaskcreates新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.

这篇关于java.lang.StackOverflowError试图在AsyncTask的解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:19