简体   繁体   English

AsyncTask停止应用程序

[英]AsyncTask stops application

I'm using AsyncTask in my application, but when it starts, it's like that it never ends. 我在我的应用程序中使用AsyncTask,但是当它启动时,它就像它永远不会结束。 When I'm debugging after doInBackground application freezes and stuck in ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) . 当我在doInBackground应用程序冻结并且陷入ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker)之后doInBackground调试时。

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! 先感谢您!

You're trying to create an AlertDialog by providing a null Context . 您正尝试通过提供null Context来创建AlertDialog 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: 您需要做的是通过将Context传递给任务的构造函数,然后将其与Dialog一起使用,从调用AsyncTaskActivity传递一个有效的Context:

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. 另外:虽然与这个问题没有直接关系,但我认为这里提到的重要性要比你在新线程中设置一个断点,就像OP一样,你不会碰到它 - 你最好用Log来跟踪进入/退出方法/代码块。

它接缝你应该得到NPE。

    Elements valueElement = result.select("div#qwidget_lastsale");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM