1

我有一个活动的基类和一个扩展基类的子类。超类有一个异步任务来执行一些操作。我通过在 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");
}

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

4

1 回答 1

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();
        }
    }
}
于 2012-10-25T05:54:01.947 回答