Progress dialog and AsyncTask error

前端 未结 3 443
醉梦人生
醉梦人生 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: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
    {
        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
        }
    

    }

提交回复
热议问题