ProgressBar不会立即显示在android中

时间:2012-10-25 05:43:31

标签: android

我有一个活动的基类和一个扩展基类的子类。超类具有执行某些操作的异步任务。我通过在ui线程上运行它来调用它,否则会抛出IllegalInitializerError:

superclass.this.runOnUiThread(new Runnable() {
    public void run() {
        String p="";
        try {
            p=new asynctasker().execute().get();
        }
    }
}

在我的异步任务中:

protected void onPreExecute()
{
    // TODO Auto-generated method stub
    super.onPreExecute();
    //showDialog();
    Log.d("Now","Inside right now");
    dialog = ProgressDialog.show(class_create_event.this, "Loading1", "Please Wait");
}

但是,对话框几乎在请求结束时显示。我部分打印正确。我知道有些东西阻止了我的ui线程。但是,如果我不从UI线程调用异步任务,则会抛出非法的初始化程序错误。有什么出路吗?

1 个答案:

答案 0 :(得分:1)

您不需要使用UIthread来调用AsyncTask

这样称呼

FetchRSSFeeds async = new FetchRSSFeeds();
async.execute();


private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

    /** progress dialog to show user that the backup is processing. */
    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage(getResources().getString(
                R.string.Loading_String));
        this.dialog.show();
    }

    protected Boolean doInBackground(final String... args) {
        try {

            // Fetch the RSS Feeds from URL
            // do background process

            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            // Setting data to list adaptar
            setListData();
        }
    }
}