Skip to content Skip to sidebar Skip to footer

Progress Dialog And Asynctask Error

I'm a bit new with the AsyncTask and ProgressDialog and i'm getting a null pointer exception error whenever i call new MyTask().execute(); on my button does my approach is not righ

Solution 1:

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

publicclassMyTaskextendsAsyncTask<Void, Integer, Void>{

Context context;
String str_mesg;
ProgressDialog progress;
 publicMyTask(Context context, String str_mesg){
  this.context=context;
  this.str_mesg=str_mesg;
 }
@OverrideprotectedvoidonPreExecute() {
    //Show progress Dialog heresuper.onPreExecute();

      // create ProgressDialog here ...
      progress = newProgressDialog(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(); 
     }
 });

Solution 2:

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 :

classYourClassextendsAsyncTask<...>
{
    ProgressDialog progress;

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

    }
    protectedvoidonPostExecute(...){
        // dismiss dialog here
        progress.dismiss();
    }
}

Solution 3:

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

publicclassMyTaskextendsAsyncTask<Void, Integer, Void>
{
    privateProgressDialog progressDialog;
    privateString mstepDescription;
    protected int mprogressPercentage;

    privateString mstepDescription;
    protected int mprogressPercentage;

    Handler handle = newHandler() {
        @OverridepublicvoidhandleMessage(Message msg) {
            super.handleMessage(msg);

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

        }
    };

    voidStep (StringStepDescription, int Progress)
    {
        mstepDescription = StepDescription;
        mprogressPercentage = Progress;
       handle.sendMessage(handle.obtainMessage());
    }


    ProtectedvoidonPreExecute()
    {
         // create dialog here
        progress = newProgressDialog (...);
        progress.setMessage(...);
        progress.show();
     }

     protectedvoidonPostExecute(...){
        // dismiss dialog here
        progress.dismiss();
     }

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

}

Post a Comment for "Progress Dialog And Asynctask Error"