【问题标题】:Android async progress dialog delayedAndroid异步进度对话框延迟
【发布时间】:2011-07-29 15:11:22
【问题描述】:

我有一个进度对话框类,它是我的主要活动的子类。它本身运行良好。

public class UpdateFeedTask extends AsyncTask<Void, Void, Void> {

    ProgressDialog loading;
    @Override
    protected void onPreExecute() { 
        loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    @Override
    protected void onProgressUpdate(Void... progress) {

    }


    @Override
    protected void onPostExecute(Void result) {

        loading.dismiss();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }

}

我有一个名为 getNews 的方法,它发送一个 http post 请求并解析结果,这需要几秒钟。在此方法中,我调用 UpdateFeedTask 以异步显示进度对话框,但它似乎不会同时进行。进度对话框似乎仅在 http 请求完成后才打开,因此它似乎首先完成了 getNews 方法,然后短暂显示进度对话框,而不是显示进度对话框并发送请求。如何在获取数据之前显示对话框?这是我如何调用进度对话框

public void getNews(){

            //show the progress dialog
    new UpdateFeedTask().execute();


    //go fetch some data
    WheruClient newsFeedLocation = new WheruClient();

    try {

        newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when);


    }catch (ClientProtocolException e) {
        Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
    }catch (JSONException e) {
        Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

**EDIT 新的异步类在后台发送 http 请求并取回 json 数组,但我收到一个致命错误

04-07 15:33:02.967: ERROR/AndroidRuntime(11004): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

我读了你的例子,我想我错过了一些关于在后台工作的东西,有什么想法吗?

public class UpdateFeedTask extends AsyncTask<Void, Void, JSONArray> {


    @Override
    protected void onPreExecute() { 
        //loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    @Override
    protected void onProgressUpdate(Void... progress) {

    }


    @Override
    protected void onPostExecute(JSONArray result) {

        //loading.dismiss();
        updateFeed(result);
    }

    @Override
    protected JSONArray doInBackground(Void... params) {

        WheruClient newsFeedLocation = new WheruClient();

        try {
            newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return newsFeedArray;
    }

}

【问题讨论】:

    标签: android progress android-progressbar


    【解决方案1】:

    需要在 doInBackground 中调用时间密集型任务。我在您的 doInBackground 方法中看不到任何内容。请注意,不要触摸 doInBackground 中的 UI。我有一些示例代码here.

    【讨论】:

    • 谢谢,我改变了结构以在 doInBackground 方法中完成工作。现在似乎打开了进度对话框,去获取 json 数组,但很快就崩溃了。不太清楚是什么问题。
    • @Brian Perin 也许 doInBackground 中的调用在线程中引发了异常。我会将 doInBackground 中的调用包装到 try catch e 和 Log.d(TAG,"msg",e) 中,以查看导致应用程序崩溃的原因。
    猜你喜欢
    • 2016-05-17
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多