Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask stops application

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!

like image 352
nabroyan Avatar asked Mar 10 '26 16:03

nabroyan


1 Answers

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.

like image 173
whitfin Avatar answered Mar 12 '26 08:03

whitfin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!