android AsyncTask - 无法执行onPostExecute方法

时间:2012-07-10 02:16:39

标签: android android-asynctask

我编写了一个运行AsyncTask的应用程序,部分代码如下所示。问题是当AsyncTask开始执行代码时 - “new AddImageTask()。execute();”在线程处理程序中,任务将启动,一切似乎都正确。但是,在执行“doInBackground”方法中的所有代码后,应用程序最终将保留在“doInBackground”方法中。任务不能转到“onPostExecute”方法。 (即不能解除对话......)出了什么问题?

感谢您的帮助......

private Handler handleFetchResult = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        progressDialog.dismiss();
        Log.d(TAG, "Start handle fetch result");
        try {
            JSONArray ja = new JSONArray(fetchResult);
            Log.d(TAG, "JSON Array Length = " + ja.length());
            JSONObject jo = new JSONObject();

            for (int i = 0; i < ja.length(); i++) {
                jo = ja.getJSONObject(i);
                PhotoURLs.add(PAT_url + jo.getString("filePath"));
                Log.d(TAG, PhotoURLs.get(i));
            }
        } catch (JSONException e) {
            Log.d(TAG, "Fetch result error: " + e.getLocalizedMessage());
            e.printStackTrace();
        }

        //TODO: display thumbnail
        new AddImageTask().execute();

    }//void handleMessage
};//Handler handleFetchResult


class AddImageTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loadThumbnailDialog.show(SitePhotoGallery.this, "Fetch thumbnails from server",
                "Loading...", true, true);
        Log.d("AddImageTask.onPreExecute","onPreExecute");
    }
    @Override
    protected Void doInBackground(Void... unused) {
        // TODO Auto-generated method stub
        for (String url : PhotoURLs) {

            String filename = url.substring(url.lastIndexOf("/") + 1, url.length());

            String thumburl = url.substring(0, url.lastIndexOf("/")+1);



            imgAdapter.addItem(LoadThumbnailFromURL(thumburl + filename));
            publishProgress();

        }
        Log.d("AddImageTask.doInBackground","doInBackground");
        return null ;
    }

    @Override
    protected void onProgressUpdate(Void... unused) {
        super.onProgressUpdate();
        imgAdapter.notifyDataSetChanged();
        Log.d("AddImageTask.onProgressUpdate","OnProgressUpdate");

    }

    protected void onPostExecute(Void... unused) {
        super.onPostExecute(null);
        loadThumbnailDialog.dismiss();
        Log.d("AddImageTask.onPostExecute","onPostExecute");

    }

}

1 个答案:

答案 0 :(得分:1)

我认为这是因为onPostExecute()应该采用Void参数而不是Void...参数。 (您还应该指定@Override,因为Soxxeh在上面的评论中指出了。)