在asynctask调用期间未显示进度对话框

时间:2016-04-19 00:37:01

标签: android android-asynctask progressdialog

好的,所以我正在尝试获取一个进度对话框,以显示何时从互联网上下载数据并应用于用户界面。我的asynctask工作正常,它运行所有步骤,但它永远不会显示对话框。我不知道我在这里做错了什么,而且我几乎碰到了墙。我甚至试图将异步任务放入新的可运行线程并像这样运行,也没有显示对话框。我正在调用像这样的异步任务

    new runningMan().execute();

以下是我尝试运行的代码。

private class runningMan extends AsyncTask<Void, Void, Integer>
{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        Log.d("Runningman: ", "Started running");
        //this method will be running on UI thread
        progress = ProgressDialog.show(PromotionMain.this, "Loading", "PleaseWait", true);

    }
    @Override
    protected Integer doInBackground(Void... params) {
        //parse the JSON string
        JSONParser jp = new JSONParser();
        try {
            Log.d(username , password);
            jp.parsesData(promos, myJson, pictureArray, pathArray, labelArray);
            Log.d("Runningman: ", "Finished parsing");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }
    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        ArrayList<ListItem> listData = getListData();
        fillListView(listData);
        Log.d("Runningman: ", "Finished runing");
        //this method will be running on UI thread
        progress.dismiss();
    }

}

感谢您对此提供任何帮助。

1 个答案:

答案 0 :(得分:1)

Call&#34; super.onPreExecute();&#34;和&#34; super.onPostExecute(结果);&#34;在您的进度代码对话框之后。或者更好的是,摆脱它们(如果你没有理由给它们打电话)。

使用以下代码:

private class runningMan extends AsyncTask<Void, Void, Integer>
{

    @Override
    protected void onPreExecute() {

        Log.d("Runningman: ", "Started running");
        //this method will be running on UI thread
        progress = ProgressDialog.show(PromotionMain.this, "Loading", "PleaseWait", true);
        super.onPreExecute();

    }
    @Override
    protected Integer doInBackground(Void... params) {
        //parse the JSON string
        JSONParser jp = new JSONParser();
        try {
            Log.d(username , password);
            jp.parsesData(promos, myJson, pictureArray, pathArray, labelArray);
            Log.d("Runningman: ", "Finished parsing");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }
    @Override
    protected void onPostExecute(Integer result) {
        ArrayList<ListItem> listData = getListData();
        fillListView(listData);
        Log.d("Runningman: ", "Finished runing");
        //this method will be running on UI thread
        progress.dismiss();
        super.onPostExecute(result);
    }

}