Android Honeycomb: Fragment not able to start AsyncTask?

狂风中的少年 提交于 2019-12-04 04:15:31

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/6412464/android-honeycomb-fragment-not-able-to-start-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!