3

I'm using AsyncTask in my application, but when it starts, it's like that it never ends. When I'm debugging after doInBackground application freezes and stuck in ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker).

Here's the structure of my code

class FetchSymbolInfo extends AsyncTask<String, Void, Document> {

        private Exception exception = null;

        @Override
        protected Document doInBackground(String... params) 
        {
            try 
            {
                Document doc = null;
                //doc = Jsoup.connect(params[0]).get();//application freezes even if I remove this line
                return doc;
            } 
            catch (Exception e) 
            {
                this.exception = e;
                return null;
            }
        }

        @Override
        protected void onPostExecute(Document result) 
        {
            if(exception != null)
        {
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(null).create();//////////
            alertDialog.setTitle("Error");
            alertDialog.setMessage("Could not fetch from internetd\nPlease check your inernet connection and symbol name.");
            alertDialog.show();
            return;
        }

        Elements valueElement = result.select("div#qwidget_lastsale");
        String valueString = valueElement.toString();
        //getting number from string
        }

        @Override
          protected void onPreExecute() {
          }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onCancelled(Document result) {
            super.onCancelled(result);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

And I'm trying to execute it like this

String url = "url";  //writing an url
FetchSymbolInfo fetch = new FetchSymbolInfo();
fetch.execute(url);

Any ideas? Thank you in advance!

crocboy
  • 2,887
  • 2
  • 22
  • 25
nabroyan
  • 3,225
  • 6
  • 37
  • 56
  • 2
    Is this your actual code, or just a skeleton? I don't see anything here that should cause it to hang. It just sends a `null` to `onPostExecute` and stops. – Geobits Jul 29 '13 at 18:59
  • It's just a skeleton, but I'm trying my code with the same body of `doInBackground` function as written here and it freezes again – nabroyan Jul 29 '13 at 19:02
  • show the code for `@Override protected void onPostExecute(Document result) { if(exception != null) { //print error } //doing some stuff }` – wtsang02 Jul 29 '13 at 19:08
  • I just edited my post. But problem is that it never reach to `onPostExecute` – nabroyan Jul 29 '13 at 19:13
  • @nabroyan Are you sure it doesn't just stop inside that method rather than not reaching? Add `Log.i("onPostExecute", "Started"); to the first line in that method and see if it's outputted in the Logcat – whitfin Jul 29 '13 at 20:25
  • @Zackehh9lives I put a breakpoint on the first line of `onPostExcucute` method, and during debugging I never reached to that breakpoint – nabroyan Jul 30 '13 at 07:53
  • 1
    @nabroyan If it's running on a new thread, does a breakpoint still get caught by the debugger? – whitfin Jul 30 '13 at 17:15
  • @Zackehh9lives you're right, as it was another thread breakpoint is not being caught by debugger (as Visual Studio does). – nabroyan Jul 31 '13 at 06:37
  • @nabroyan Then use `Log` as I first suggested, that should help you out. Also, `AlertDialog.Builder(context)` is what you need, with context being passed from the calling Activity. – whitfin Jul 31 '13 at 06:46
  • @Zackehh9lives I've already tried that and it worked. Now I use `Log` and `context` instead of `null`. Thank you!! – nabroyan Jul 31 '13 at 06:51
  • @nabroyan So your question is answered? :) – whitfin Jul 31 '13 at 06:54

2 Answers2

1

You're trying to create an AlertDialog by providing a null Context. What you need to do is pass a valid Context from the Activity calling the AsyncTask by passing the Context to the constructor of the task and then using that with the Dialog:

class FetchSymbolInfo extends AsyncTask<String, Void, Document> {

private Context parent;

// ...

public FetchSymbolInfo(Context c){
    parent = c;
}

// ...

if(exception != null){
    AlertDialog alertDialog;
    alertDialog = new AlertDialog.Builder(parent).create();
    alertDialog.setTitle("Error");
    alertDialog.setMessage("Could not fetch from internetd\nPlease check your inernet connection and symbol name.");
    alertDialog.show();
    return;
}

Addition: Although not directly related to the question, I think it's important to mention here than if you set a breakpoint in a new thread as the OP did, you won't hit it - you're better off using Log to keep track of entering/exiting methods/code blocks.

whitfin
  • 4,539
  • 6
  • 39
  • 67
0

It seams that you should be getting a NPE in.

    Elements valueElement = result.select("div#qwidget_lastsale");
Hadriel
  • 159
  • 1
  • 13
  • Excuse me, what do you mean by saying NPE? – nabroyan Jul 30 '13 at 07:51
  • @nabroyan NullPointerException – whitfin Jul 30 '13 at 17:14
  • @Zackehh9lives that's right I returned `null` and `exception` was `null` to and I tried to work with result. And if I an exception occurred`alertDialog = new AlertDialog.Builder(null).create();` line caused NPE. So there was lot of `null` in this piece of code – nabroyan Jul 31 '13 at 06:38