1

私はAsyncTaskと仲良くしようとしています..私の問題は、プロシージャの出力に基づいてテキストビューのテーブルを動的に作成していたことでした..しかし、asynctaskを使用することで、より効率的な方法でそれを行うことができると考えました..だから、私がしたことは次のとおりです:

private class DisplayReport extends AsyncTask<Void, Void, Boolean>{
    protected void onPreExecute(){
        //Message -- "Please wait while the Report Loads..."
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        //Here i fetch the data from the procedure via a web service
        //parse the result of web service and set a bool variable true or false based on whether the dataset fetched is empty or not.
    }
    protected void onPostExecute(Boolean value){
        if(value == true){
                 "Please try again later!!"
        }
        else{
                 runOnUiThread(GenTable);
        }
    }
    private Runnable GenTable = new Runnable(){
        public void run(){
            try {
                displayReport(result); // in this method i build the table.
            } catch (Exception e) {
                ad.setTitle("Error..");
                ad.setMessage(e.toString());
            }
        }
    };
}

上記の非同期クラスは、アクティビティを拡張するメイン クラスの内部クラスです。そして、これが私がasynctaskを実行している方法です..

DisplayReport dr = new DisplayReport();
dr.execute();

今、デバッグする"source not found" error on dr.execute().. と、ネットでたくさん検索しようとしましたが、具体的なものは何も見つかりません。また、私のアプローチが間違っている場合はお知らせください。

ありがとう!

4

2 に答える 2

0

onPostExecuteはすでにUIスレッドで実行されているため、別の実行可能ファイルを作成しないでください。このようにしてくださいonPostExecute

 protected void onPostExecute(Boolean value){
            if(value == true){
                     String message = "Please try again later!!";
                     // Do something here with your message
            }
            else{
                     displayReport(result);
            }
        }
于 2013-01-18T06:59:45.557 に答える
0

Execute は新しいスレッドを開始しようとしています。あなたはそれにデバッグしたくありません。代わりに、onPreExecute、doInBackground、および onPostExecute にブレークポイントを配置すると、それぞれがいつ呼び出されるかを確認できます。

于 2013-01-18T06:54:19.623 に答える