考虑下面的异步任务:
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
中成功打印。
为什么有时候不工作?
答案 0 :(得分: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);
}