Progress dialog and AsyncTask error

大兔子大兔子 提交于 2019-12-02 08:58:20

use class MyTask constructor for passing Activity Context to initialize ProgressDialog and message which u want to show in ProgressDialog inside onPreExecute as :

public class MyTask extends AsyncTask<Void, Integer, Void>{

Context context;
String str_mesg;
ProgressDialog progress;
 public MyTask(Context context, String str_mesg){
  this.context=context;
  this.str_mesg=str_mesg;
 }
@Override
  protected void onPreExecute() {
    //Show progress Dialog here
      super.onPreExecute();

      // create ProgressDialog here ...
      progress = new ProgressDialog(context);
      progress.setMessage(str_mesg);
      // set other progressbar attributes
       ....
      progress.show();

    }

 // your code here....

and start AsyncTask as on Button Click :

button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       // TODO Auto-generated method stub
        new MyTask(Your_Current_Activity.this).execute(); 
     }
 });

Sounds like progress dialog is not getting initialized .So just make sure it is initialized correctly.

protected void onPreExecute() {
                    super.onPreExecute();
                    //initialize progress dialog here
                    progress.show();
                }

EDIT :

class YourClass extends AsyncTask<...>
{
    ProgressDialog progress;

    Protected void onPreExecute(){
        // create dialog here
      progress = new ProgressDialog (...);
      progress.setMessage(...);
      progress.show();

    }
    protected void onPostExecute(...){
        // dismiss dialog here
        progress.dismiss();
    }
}

nother part that I hope it helps ...if u want to change the text and percentage of progress during the background task u can define a handler

public class MyTask extends AsyncTask<Void, Integer, Void>
{
    private ProgressDialog progressDialog;
    private String mstepDescription;
    protected int mprogressPercentage;

    private String mstepDescription;
    protected int mprogressPercentage;

    Handler handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            progressDialog.setMessage(mstepDescription);
            progressDialog.setProgress(mprogressPercentage);

        }
    };

    void Step (String StepDescription, int Progress)
    {
        mstepDescription = StepDescription;
        mprogressPercentage = Progress;
       handle.sendMessage(handle.obtainMessage());
    }


    Protected void onPreExecute()
    {
         // create dialog here
        progress = new ProgressDialog (...);
        progress.setMessage(...);
        progress.show();
     }

     protected void onPostExecute(...){
        // dismiss dialog here
        progress.dismiss();
     }

   @Override
    protected Void doInBackground(Void... params)
    {
          // do somthing 
          Step("...", 40);
          // do something else
    }

}

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