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

时间:2013-02-14 16:49:05

标签: android android-progressbar android-context

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

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

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();

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

我的错误如下: enter image description here

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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