public class async extends AsyncTask<String, Integer, String>{
ProgressDialog prog;
@Override
protected void onPreExecute() {
super.onPreExecute();
prog=new ProgressDialog(async.this);//This is chowing error
prog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prog.setMax(100);
prog.show();
}
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 10; i++) {
publishProgress(5);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... values) {
prog.setProgress(values[0]);
super.onProgressUpdate(values);
}
}
The above code is producing the error:
the constructor ProgressDialog(AndroidasynctaskActivity.async) is
undefined
Why is this so? Can anyone please help me troubleshoot this?
As already mentioned, the reason this is happening is because the
ProgressDialogconstructor you’re using needs aContextobject. Here’s one example of how you can do this.Modify your
asyncclass and add a single-argument constructor that accepts aContextobject. Then modify theonPreExecutemethod to use saidContext. For example:Then to instantiate and run this
AsyncTask: