Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressDialog not working in external AsyncTask

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? [Edited much later...the answer is False and I'm not sure if this is a bug or what. I'm using Android 1.5. I'm going to read up on Services.]

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
    }
 }

Here's the stack trace from the emulator:

----- pid 846 at 2010-03-21 19:58:25 -----
Cmd line: com.trial

DALVIK THREADS:
"main" prio=5 tid=3 NATIVE
 | group="main" sCount=1 dsCount=0 s=0 obj=0x40018e70
 | sysTid=846 nice=0 sched=0/0 handle=-1098855268
 at android.os.BinderProxy.transact(Native Method)
 at      android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:2103)
 at com.android.internal.os.RuntimeInit.crash(RuntimeInit.java:302)
 at   com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:75)
 at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:887)
 at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:884)
 at dalvik.system.NativeStart.main(Native Method)

 "Binder Thread #3" prio=5 tid=15 NATIVE
 | group="main" sCount=1 dsCount=0 s=0 obj=0x43733d88
 | sysTid=852 nice=0 sched=0/0 handle=1486928
 at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #2" prio=5 tid=13 NATIVE
 | group="main" sCount=1 dsCount=0 s=0 obj=0x437313c8
 | sysTid=851 nice=0 sched=0/0 handle=1492472
 at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=11 NATIVE
 | group="main" sCount=1 dsCount=0 s=0 obj=0x4372b9b0
 | sysTid=850 nice=0 sched=0/0 handle=1492664
 at dalvik.system.NativeStart.run(Native Method)

"JDWP" daemon prio=5 tid=9 VMWAIT
 | group="system" sCount=1 dsCount=0 s=0 obj=0x4372a2a0
 | sysTid=849 nice=0 sched=0/0 handle=1490176
 at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
| group="system" sCount=0 dsCount=0 s=0 obj=0x4372a1e8
| sysTid=848 nice=0 sched=0/0 handle=1487888
 at dalvik.system.NativeStart.run(Native Method)

"HeapWorker" daemon prio=5 tid=5 VMWAIT
 | group="system" sCount=1 dsCount=0 s=0 obj=0x427d03c0
 | sysTid=847 nice=0 sched=0/0 handle=1487592
 at dalvik.system.NativeStart.run(Native Method)

 ----- end 846 -----

What am I doing wrong?

Mr. Snowflake,

Tried:

@Override
protected void onPreExecute()
{

Activity.this.runOnUiThread(new Runnable() {
      public void run() {
        ProgressDialog progressDialog = new ProgressDialog(context);
        //crashes with the following line
        progressDialog.show(context, "Working..", "Retrieving info");
      }
    });
}

Activity.this is flagged as an error: No enclosing instance of the type Activity is accessible in scope

I am thinking I need to FillDB extends Activity then create a private class within FillDB extending AsyncTask? That wont' work. No assurance when the FillDB activity will start and can't use startActivityForResult since no result is returned from AsyncTask when it is completed.

Update: Tried creating a private class in the calling class. Still can't show a ProgressDialog. One of the errors is: Unable to add window -- token null is not for an application. I have no idea what token is being referred to.

like image 847
eric Avatar asked Mar 21 '10 16:03

eric


2 Answers

Have you considered overriding the onCreateDialog() method to implement your Dialogs? It's easy to create and control them this way as Android does most of the work for you. I have an example below doing exactly what you're trying to do.

http://pastie.org/880540

like image 100
Jason Knight Avatar answered Oct 23 '22 03:10

Jason Knight


I gave up on AsyncTask and used a Handler to handle the Thread. All is good.

Not sure if it's a bug in 1.5 with AsyncTask or something else I did not do.

like image 33
eric Avatar answered Oct 23 '22 04:10

eric