ProgressDialog not working in external AsyncTask
- by eric
I'm beginning to think that to get a ProgressDialog to work the AsyncTask has to be an inner class within an Activity class.  True?
I have an activity the uses a database to manipulate information.  If the database is populated all is well.  If it is not populated then I need to download information from a website, populate the database, then access the populated database to complete the Views in onCreate.
Problem is without some means to determine when the AsyncTask thread has finished populating the database, I get the following Force Close error message:  Sorry!  The application has stopped unexpectedly.  I click on the Force Close button, the background AsyncTask thread continues to work, the database gets populated, and everything works ok.
I need to get rid of that error message and need some help on how to do this.  Here's some psuedo code:
public class ViewStuff extends Activity
{
  onCreate
  { 
    if(database is populated)
      do_stuff
    else
    {
      FillDB task = null;
      if(task == null || task.getStatus().equals(AsyncTask.Status.FINISHED))
       {                
         task = new FillDB(context);
         task.execute(null);
       }
    }
  continue with onCreate using information from database to properly display 
 } // end onCreate
} // end class
In a separate file:
public class FillDB extends AsyncTask<Void, Void, Void>
{
    private Context context;
    public FillDB (Context c)  //pass the context in the constructor
{
    context = c;
}
    public void filldb ()
    {
      doInBackground();
    }
    @Override
    protected void onPreExecute()
    {          
       ProgressDialog progressDialog = new ProgressDialog(context);
       //crashes with the following line
       progressDialog.show(context, "Working..", "Retrieving info");
    }
    @Override
    protected Void doInBackground(Void... params)
    {
  // TODO Auto-generated method stub
try
     etc etc etc
    }
 }
What am I doing wrong?