3

私は自分のアプリケーションで AsyncTask を使用していますが、開始すると終了しないようです。doInBackgroundアプリケーションがフリーズしてスタックした後にデバッグしているときThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker)

これが私のコードの構造です

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);
        }

そして、私はこのようにそれを実行しようとしています

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

何か案は?前もって感謝します!

4

2 に答える 2

1

AlertDialogを提供して を作成しようとしていますnull Context。あなたがする必要があるのは、コンテキストをタスクのコンストラクターに渡し、それをダイアログで使用することにより、Activity呼び出しから有効なコンテキストを渡すことです。AsyncTask

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;
}

追加:質問とは直接関係ありませんが、OPが行ったように新しいスレッドにブレークポイントを設定した場合よりも、ここで言及することが重要だと思います。ヒットしない-Log入力を追跡するために使用する方が良い/終了メソッド/コード ブロック。

于 2013-07-31T07:04:35.583 に答える
0

NPEを取得する必要があるようです.

    Elements valueElement = result.select("div#qwidget_lastsale");
于 2013-07-29T20:23:57.477 に答える