1

考虑下面的异步任务:

protected class FetchArticlesAsyncTask extends AsyncTask<Category, Integer, HashMap<String, Object>> 
{
    private final String TAG = FetchArticlesAsyncTask.class.getSimpleName();
    private HashMap<String, Object> data;
    Bundle bundle = new Bundle();

    protected HashMap<String, Object> doInBackground(Category... categories) 
    {
        HashMap<String, Object> result = new HashMap<String, Object>();

        Articles articles = ... // Get sth from HTTP
        result.put("articles", articles);
        Log.d(TAG, "result = " + result); // Print out sth which is OK

        return result;
    }

    protected void onPostExecute(HashMap<String, Object> result) 
    {
        notifyAsyncTask(FetchArticlesAsyncTask.class, result, this.data);                
    }


..

在我的来电者中,我有

protected void notifyFetchArticlesAsyncTask(Class<?> c, Object result, Object input)
{
    HashMap<String, Object> data = (HashMap<String, Object>) result;
    Log.d(TAG, "result = " + result); // result is SOMETIMES empty map? WHY
}

问题只是有时我会得到空地图notifyFetchArticlesAsyncTask,但结果可以成功打印 doInBackground

为什么有时不工作?

4

1 回答 1

0

你有没有尝试过这样的事情

protected Object doInBackground(Category... categories) 
{
    Articles articles = ... // Get sth from HTTP
    return articles;
}
protected void onPostExecute(Object articles) 
{
     HashMap<String, Object> result = new HashMap<String, Object>();
     result.put("articles", articles);
     Log.d(TAG, "result = " + result); // Print out sth which is OK

    notifyAsyncTask(FetchArticlesAsyncTask.class, result, this.data);                
}
于 2013-11-03T19:07:00.393 回答