0

所以我正在尝试从异步任务启动进度对话框。

在我的主要活动中,我正在调用异步任务,如下所示:

new OCRScanAsyncTask(getApplication().getApplicationContext()).execute();

也试过:

new OCRScanAsyncTask(getBaseContext()).execute();

我的 asyncTask 看起来像这样:

public class OCRScanAsyncTask extends AsyncTask<String, Integer, Boolean> {

    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    private ProgressDialog progressDialog;
    private String TAG ="OCRAsyncTask";
    private String IMAGE_PATH = Environment.getExternalStorageDirectory() + "/bcCardReader/tmp/tmpCrd.jpg";
    public static final String DATA_PATH = Environment
            .getExternalStorageDirectory().toString() +"/bcCardReader/tmp/";

    private String lang = "eng";
    private Context context;


    public OCRScanAsyncTask(Context c){
       Log.v(TAG, "*****************CONSTRUCTING");
        context = c;
        progressDialog = new ProgressDialog(context);
    }
    @Override
    protected void onPreExecute(){

        super.onPreExecute();
        Log.v(TAG, "pre exe");
        progressDialog.setTitle("Please Wait");
        progressDialog.setMessage("Performing Character Recognition");
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);
        progressDialog.show();

    }
............

我的错误如下所示: 在此处输入图像描述

4

2 回答 2

1

异步任务中progressDialog上的错误窗口令牌

很可能是Context变量的问题。所以我的第一个建议是更换你的线路

new OCRScanAsyncTask(getApplication().getApplicationContext()).execute();

new OCRScanAsyncTask(YourActivityName.this).execute();


注意:我建议您progressDialog在开始之前显示AsyncTask

ProgressDialog pd = new ProgressDialog(this); // or YourActivityName.this
pd.setMessage("some message");
pd.show();
AsyncWorker w = new AsyncWorker();
w.execute();
于 2013-02-14T17:15:34.283 回答
0

看起来通过构造函数传递的上下文似乎是 ApplicationContext ...

于 2013-02-14T16:52:11.107 回答