开发者

Android Honeycomb: Fragment not able to start AsyncTask?

I've run into this error before, but thought it was some mistake by the strict mode system. However, it apparently was right as I sadly found out now. :(

My programm is made of one Activity and loads of Fragments. I have a NetworkWorker fragment, which starts URL requests like this:

public void startURLRequest(Fragment target, String url, String message)
{

    if (asyncTask != null) asyncTask.cancel(true);
    asyncTask = new FragmentHttpHelper(url, message, target);
    asyncTask.doInBackground();

    return;
}

FragmentHttpHelper is a custom inner class derived from AsyncTask:

private class FragmentHttpHelper extends AsyncTask<Void, Void, String>
{
    //...

    @Override
    protected String doInBackground(Void... params)
    {

        if (CheckInternet())
        {
            try
            {
                URL myURL = new URL(url);
                httpClient = new DefaultHttpClient();

                if (this.message == null)
                {
                    httpRequest = new HttpGet(myURL.toExternalForm());
                }
                else
                {
                    httpRequest = new HttpPost(myURL.toExternalForm());

                    HttpEntity myEntity = new StringEntity(message, 开发者_开发知识库"UTF-8");
                    ((HttpPost) httpRequest).setEntity(myEntity);
                }

                // and so on...
            }
            //catches
            finally
            {
                // auf jeden Fall Verbindung beenden
                if (httpRequest != null) httpRequest.abort();
                // if (httpClient != null) httpClient.close();
            }

        }
        else
        {
            showDialog(getString(R.string.net_notify_no_network), target);
        }

        //...
    }

    /**
     * gets called after AsyncTask has finished
     */
    protected void onPostExecute(String result)
    {

        if (target == null)
        {
            ((NetworkWorkerListener) getActivity()).onDownloadHasFinished((!result.contentEquals(ERROR)), result);
        }
        else
        {
            ((NetworkWorkerListener) target).onDownloadHasFinished((!result.contentEquals(ERROR)), result);
        }
    }

}

NetworkWorkerListener is just an interface for a callback on the Fragment which started the URL request. This class has always worked fine when I used it in my 2.2 app. I would derive it in my Activities then.

Now, if a menu item is selected, another worker Fragment starts the URL request via the above method and opens a loading dialog:

FragmentManager fragmentManager = getFragmentManager();

NetworkWorker network = (NetworkWorker) fragmentManager.findFragmentByTag(TabletMain.NETWORK);
if (network == null) return WorkerFeedback.NO_NETWORK_WORKER;

myDialog = LoadingDialog.createInstance(getString(R.string.notify_download), this);
myDialog.show(fragmentManager, TabletMain.ONETIME);

network.startURLRequest(this, someurl, null);

At least that's what supposed to happen. Instead, when I click the menu item, my app freezes and no loading dialog is shown until. Next happening is the reaction to the end of the download (or, in my case an error message, as I am sending nonsense strings). Meaning onPostExecute() was reached.

I feel really stuck now - is it not possible to use AsyncTask with Fragments? Or did I do something wrong?

Thanks for your help, jellyfish


Don't call doInBackground directly, call execute instead (on the async task)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜