-3

Though I have checked many references online, I still can't find the problem. It seems the ProgressDialog appears fine but whenever I want to update the progress, in onProgressUpdate its instance is always null.

This is my AsyncTask:

package com.async_tasks;

public class UploadTask extends AsyncTask<Void,Integer,Void> implements Serializable {        
    private static final String TAG = UploadTask.class.getSimpleName();
    private ConnectionToServer _connectionToServer;
    private TransferDetails _td;
    private Activity _activity;
    private ProgressDialog _progDialog;
    private UploadTask _taskInstance;

    public UploadTask(Activity activity, ConnectionToServer connectionToServer, TransferDetails td) {

        _activity = activity;
        _connectionToServer = connectionToServer;
        _td = td;
        _taskInstance = this;
    }

    @Override
    protected void onPreExecute() {

        _progDialog = new ProgressDialog(_activity);
        String cancel = _context.getResources().getString(R.string.cancel);

        _progDialog.setCancelable(false);
        _progDialog.setTitle(_context.getResources().getString(R.string.uploading));
        _progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        _progDialog.setProgress(0);
        _progDialog.setMax(100);
        _progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                _taskInstance.cancel(true);
            }
        });

        _progDialog.show();

    }

    @Override
    protected Void doInBackground(Void... voids) {

        //uploading file ...
                float percent = (float) (fileSize - bytesToRead) / fileSize * 100;
                publishProgress((int)percent);                   
            }

        }
         catch (IOException e) {
               // Handling exception
        } finally {

            if(bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            clearMembers();

            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {

        if(_progDialog!=null) { // <<------------ I suspect for some reason this is always false, as _progDialog is always null - But why?!
            _progDialog.incrementProgressBy(progress[0]);
        }
    }

    @Override
    protected void onPostExecute(Void result)    {

        //The task is complete, clear members
        clearMembers();
    }

    private void clearMembers() {

        _activity = null;
        if(_progDialog!=null) {
            _progDialog.dismiss();
            _progDialog = null;
        }
    }
}

And this is the call from MainActivity:

TransferDetails td = (TransferDetails) report.data();
ConnectionToServer conn  = StorageServerProxyService.getConn();
UploadTask uploadTask = new UploadTask(MainActivity.this, conn, td);
uploadTask.execute();
Nom1fan
  • 846
  • 2
  • 11
  • 27
  • This is not a duplicate of http://stackoverflow.com/questions/4538338/progressdialog-in-asynctask since there is no mention there of trying to work with progressDialog inside the onProgressUpdate hook method. This is the problematic method for me in which I get a null reference to the progressDialog no matter what I tried... – Nom1fan Apr 13 '16 at 10:41

1 Answers1

0

Modify the onPreExecute() method as below :

@Override
    protected void onPreExecute() {

 progDialog = new ProgressDialog(ActivityName.this);
        String cancel = _context.getResources().getString(R.string.cancel);

        _progDialog.setCancelable(false);
        _progDialog.setTitle(_context.getResources().getString(R.string.uploading));
        _progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        _progDialog.setProgress(0);
        _progDialog.setMax(100);
        _progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                _taskInstance.cancel(true);
            }
        });

        _progDialog.show();

    }
Ameer Faisal
  • 355
  • 1
  • 9
  • This AsyncTask is not inside the MainActivity but in a separate class, so MainActivity.this is not possible here. I tried to pass the Activity context using the setter and then perform the instantiation inside onPreExecute as you suggested, but it still didn't solve my issue. So what else can I do if my AsyncTask is on a separate class? – Nom1fan Apr 13 '16 at 07:00
  • Change Context to Activity in setter method and also change the type of this._context to Activity then try and let me know – Ameer Faisal Apr 13 '16 at 07:06
  • Ok I will try it and update you. Thanks a lot for trying to help and not just giving me -1's without saying anything else like everyone else :) – Nom1fan Apr 13 '16 at 08:25
  • I did as you said but I still get _progDialog = null in my onProgressUpdate :( – Nom1fan Apr 13 '16 at 09:38
  • Don't make a setter for Activity do one thing increase one parameter in constructor of AsyncTask for Activity and pass ActivityName.this where you call asynctask then get the Activity object from there – Ameer Faisal Apr 13 '16 at 09:46
  • I instantiate this AsyncTask from a service, not from an Activity. But it seems I have no choice but to try to make some changes. Thanks a lot for your help Ameer. – Nom1fan Apr 13 '16 at 09:50
  • I changed my code to this: `UploadTask uploadTask = new UploadTask(MainActivity.this, conn, td);` but still in onProguressUpdate both the _progDialog is null and now I noticed the _activity reference is null too. All other members/references are non-null. What am I still doing wrong? – Nom1fan Apr 13 '16 at 10:05
  • I can't answer my question so I will write it here in the comments. The only thing that solved the issue eventually is to move the entire UploadTask into the MainActivity class. Nothing else helped. – Nom1fan Apr 15 '16 at 07:48