Progress dialog and AsyncTask error

前端 未结 3 440
醉梦人生
醉梦人生 2021-01-27 10:34

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 ap

相关标签:
3条回答
  • 2021-01-27 11:00

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-27 11:01

    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
        }
    

    }

    0 讨论(0)
  • 2021-01-27 11:04

    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(); 
         }
     });
    
    0 讨论(0)
提交回复
热议问题