views:

1428

answers:

5

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.

A: 

Try running

   ProgressDialog progressDialog = new ProgressDialog(context);
   //crashes with the following line
   progressDialog.show(context, "Working..", "Retrieving info");

as a runnable as argument to Activity.runOnUiThread(). User interface elements should be created and manipulated in the UI thread. I guess this is also true for Dialogs.

[edit] Code:

Activity.this.runOnUiThread(new Runable() {
  public void run() {
    ProgressDialog progressDialog = new ProgressDialog(context);
    //crashes with the following line
    progressDialog.show(context, "Working..", "Retrieving info");
  }
};
MrSnowflake
Mr Snowflake, (How do I format comments so everything's not running together?)I tried: Activity.runOnUiThread((Runnable) progressDialog.show(context, "Working..", "Retrieving info");It tells me: Can not make a static reference to the non-static method runOnUIThread(Runnable) from the type Activity.
eric
I updated the post, hope that helps.
MrSnowflake
MrSnowflake - Tried but a no go. I edited my post to show your code incorporated into mine. Thanks.
eric
A: 

What is the Context you are passing to your AsyncTask? Is it the Activity or something else?

Romain Guy
Romain,I'm passing the application context: context = getApplicationContext();if(task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) { task = new FillNYLottoDB(context); task.execute(null); }
eric
+2  A: 

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

Jason Knight
Jason, That didn't do it either. Maybe I have something else going on? Which Android version are you running?
eric
Thanks Jason...
MikeNereson
A: 

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.

eric
A: 

I was having the same problem as OP for quite sometime with a public (external) AsyncTask. When researching on the error message "attempted to add window with non-application token", I found out that the problem is in setting the context that is being passed in the constructor of the AsyncTask. The context that should be passed is "this", not this.getApplicationContext() or this.getBaseContext(). That resolved my issue.

Samik R.