1

Here's the prototype of my code:-

class something extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        String str = objectofanotherthing.execute("").get();
    }

    class anotherthing extends AsyncTask
    {
        ProgressDialog Dialog;
        protected void onPreExecute()
        {
            Dialog = ProgressDialog.show(context, "Progressing", "Working on it,");
        }

        protected String doInBackground(String... params) 
        {
            ...
        }

        protected void onPostExecute(Integer result)
        { 
            Dialog.dismiss();
        }
    }
}

When I execute my code, my program seems to stop at ProgressDialog.show, no crashes no errors. When I commented this part, the program executes fine. I'm not sure what can be done. Can anyone help me with this. Thanks in advance.

Triode
  • 11,309
  • 2
  • 38
  • 48
daemon54
  • 1,057
  • 3
  • 16
  • 36

1 Answers1

2

If you call an AsyncTask with get(), you block the UI Thread, and your app can appear to stop responding. Take the get() call out so it is just execute().

objectofanotherthing.execute("");

This means that now your execution is done independently of the UI Thread, so you cannot expect your String to hold anything useful. To get around this, make the AsyncTask a nested class inside your Activity (your snippet does this already), have doInBackground() return something useful, and in onPostExecute() dismiss the dialog and set whatever you need to set.

An alternative to the nested class is passing the Activity off to the AsyncTask or defining an interface, but if the Task is for one activity, nesting is easier.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • I did what you said and I'm having the exception:- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application null – daemon54 Mar 13 '13 at 00:05
  • @daemon54 Make sure your `context` is an instance of Activity. If you're using the nested AsyncTask in an Activity, you can do `something.this` instead of `context`. – A--C Mar 13 '13 at 00:06
  • Hey A--C, I got the result when I tried Activity.this and also get() seems to be working with it too. One more funny thing is that the progressdialog does go off automatically. I need to touch on the screen to make it go off. Any suggestion on what can I do regarding that. – daemon54 Mar 13 '13 at 00:13
  • And btw when I tried getting the result without the get(). I'm getting null values, so I had to go back to using get(). – daemon54 Mar 13 '13 at 00:14
  • @daemon54 not sure about the dialog not dismissing automatically, maybe you didn't actually override `onPostExecute()`. [Always use `@Override` annotations](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why). Also about your `null` values, it is probably due to the fact your code isn't meant to work with Threads. I said in my answer *This means that now your execution is done independently of the UI Thread, so you cannot expect your String to hold anything useful.* – A--C Mar 13 '13 at 00:17
  • Hey A--C, I followed your advice, the onPostExecute is not overridden since, it is of result = Integer (In my case result is String). When I corrected it, I got it right.Thanks. – daemon54 Mar 13 '13 at 00:30