简体   繁体   中英

Can't stop a ProgressDialog in an AsyncTask

I have two AsyncTask that are not working. There must be something in common with both I am doing wrong.

My first activity takes a long time to load so I wish to have a progressDialog/Bar but it fails. I access a AsyncTask in my onCreate with

new HeavyWorker(this).execute();


public class HeavyWorker extends AsyncTask < String , Context , Void > {

    private ProgressDialog      progressDialog ;
    private Context             targetCtx ;

    public HeavyWorker ( Context context ) {
        this.targetCtx = context ;
   //     this.needToShow = true;
        progressDialog = new ProgressDialog ( targetCtx ) ;
        progressDialog.setCancelable ( false ) ;
        progressDialog.setMessage ( "Retrieving data..." ) ;
        progressDialog.setTitle ( "Please wait" ) ;
        progressDialog.setIndeterminate ( true ) ;
    }

    @ Override
    protected void onPreExecute ( ) {
        progressDialog.show ( ) ;
    }

    @ Override
    protected Void doInBackground ( String ... params ) {
      // Do Your WORK here

These lines below work fine when in the normal
"public void onCreate(Bundle savedInstanceState)" area

        Ls = new LoadSettings(CreateAppointment.this);
        gs = new GlobalSubs();

        TimeZoneList();

This line dynamically fills a Spinner box and stops it from working, so I commented it out.

    //      createTimezone();
        ZoneList(Ls.getCountryZone());

This line also fills a spinnerbox dynamicly so I commented it out

    //  createZone();
        InitialiseUI();
        CreateFileName();

       return null ;
    }

This area is never reached?

    @ Override
    protected void onPostExecute ( Void result ) {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss ( ) ;
        }
    }
}

My second page fails with the same style of problem.
I've also tried in a settings page with slightly different code. Fixed now with:

//
private class MyClass extends AsyncTask<Void, Void, Void>{

ProgressDialog mProgressDialog;

public MyClass(){

mProgressDialog = new ProgressDialog(Preferences.this);
mProgressDialog.setMessage ("Please Wait While Saving");
mProgressDialog.setTitle("Saving");

//

@Override
 protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog.show(Preferences.this, "Saving", "Please Wait a Sec");
 }

@Override
protected Void doInBackground(Void... params)
    {
        saveSettings();
        return null;
    }

@Override
 protected void onPostExecute(Void unused) {

The only way it works is when I comment out the line and change to a different activity

//   mProgressDialog.dismiss();
        Toast.makeText(Preferences.this, "Your Settings Where Saved.", Toast.LENGTH_SHORT).show();
     gotoMainPage();
 }

}

    private void gotoMainPage(){
        Intent a = new Intent(this, Main_entry.class);
        finish();
        startActivity(a);
    }

You are not supposed to be doing anything to the UI in the background. doInBackground() runs on a background thread. If you need to update the UI during the background process, you use publishProgress() (and onProgressUpdate() ) for that.

It looks to me like you're still trying to perform UI operations here:

    //  createZone();
    InitialiseUI();
    CreateFileName();

I don't know what InitialiseUI() does, but it sounds UI-ish. If you need UI setup, do that in preExecute() , which is called on the UI thread.

Can you just set a breakpoint in your doInBackground() method, see how far it's getting, and let us know? That would help.

On your second example, you say it's failing in onPostExecute() here:

mProgressDialog.dismiss();

There's really only one way for that to fail. If mProgressDialog is null. Can you check the constructor of this class, and make sure you remembered to instantiate it?

PS You have the second generic parameter as Context in your first AsyncTask subclass. But, you don't seem to use it. Normally, that would be the type of the data that doInBackground() passes to onProgressUpdate() via publishProgress() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM