ProgressDialog using AsyncTask producing constructor undefined error

风流意气都作罢 提交于 2019-12-03 21:54:33

As already mentioned, the reason this is happening is because the ProgressDialog constructor you're using needs a Context object. Here's one example of how you can do this.

Modify your async class and add a single-argument constructor that accepts a Context object. Then modify the onPreExecute method to use said Context. For example:

public class async extends AsyncTask<String, Integer, String>{

  private Context context;
  ProgressDialog prog;

  public async(Context context) {
    this.context = context;
  }


  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    prog=new ProgressDialog(context); 
    prog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    prog.setMax(100);
    prog.show();
  }

  // ...
}

Then to instantiate and run this AsyncTask:

async mTask = new async(context);
mTask.execute(params);

Async tasks do not provide an application or activity context. You may have to pass the context in if this class is contained within the activity that called it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!