-1

Why is onProgressUpdate not being called?

public class DoStuff extends AsyncTask<Integer, Integer, String> {
    private MainActivity activity;

    public DoStuff(MainActivity a){
        activity = a;
    }

    protected String doInBackground(Integer... params) {
        Integer loops = params[0];
        for (int i = 0; i <= loops.intValue() ; i++) {

            SystemClock.sleep(1000);
            publishProgress(new Integer((i / loops) * 100));

        }
        return "Done: " + loops.intValue();
    }

    protected void onProgressUpdate(Integer progress)
    {
        activity.updateProgress("" + progress.intValue() + "%");
    }

}
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
CruelIO
  • 18,196
  • 16
  • 40
  • 58
  • protected String doInBackground(Integer... params) { Log.d("TEST", "vals "+Arrays.asList(loops)); Integer loops = params[0]; – Martin Pfeffer Jun 10 '16 at 19:31
  • 2
    In addition to answers, use the `@Override` annotation to help prevent this type of mistake. See [this question](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) for more details. – Allan Pereira Jun 10 '16 at 19:38
  • Use @Override .... Then compiler will inform you that you didn't override method... Or did it wrong... – Selvin Jun 10 '16 at 19:39

2 Answers2

1

You are using wrong signature for onProgressUpdate

Change

protected void onProgressUpdate(Integer progress)
{
    activity.updateProgress(""+progress.intValue() +"%");
}

to

protected void onProgressUpdate(Integer... progress)
{
    activity.updateProgress(""+progress[0].intValue() +"%");
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
1

You have to change:

protected void onProgressUpdate(Integer progress) {
    activity.updateProgress(""+progress.intValue() +"%");
}

to:

protected void onProgressUpdate(Integer... progress) {
   activity.updateProgress(""+progress.intValue() +"%");
}

Parameters must be a varargs of the type that you declared as second

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70