2

斯特兰利,我的AsyncTask遗嘱执行onPreExecute但不doInBackground()执行onPostExecute()。Logcat 没有错误或消息表明出现问题。

class LocationTask extends AsyncTask<String, Void, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.d(TAG, "onPreExecute============");
    }

    @Override
    protected String doInBackground(String... urls) {
        Log.d(TAG, "doInBackground============");
        Log.d(TAG, "urls[0]:" + urls[0]);
        return "test";
    }

    @Override
    protected void onPostExecute(String result){
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Log.d(TAG, "onPostExecute============");
    }
}

public class CsLocationTaskTest extends AndroidTestCase {

    public void testExecute(){
        LocationTask task = new LocationTask();
        task.execute("my_test_url", null);
    }
}

如您所见,AsycTask没有什么特别的,但是在测试用例和我doInBackground的. 我不明白发生了什么,请让我了解发生了什么问题。谢谢。onPostExecuteActivity

4

1 回答 1

0

AsyncTask需要是 的子类CsLocationTaskTest。此外,您不需要null.

根据开发文档:

"AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)"

位于:http: //developer.android.com/reference/android/os/AsyncTask.html

尝试这个:

 public class CsLocationTaskTest extends AndroidTestCase {

    public void testExecute(){
        LocationTask task = new LocationTask();
        task.execute("my_test_url");
    }

    class LocationTask extends AsyncTask<String, Void, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.d(TAG, "onPreExecute============");
    }

    @Override
    protected String doInBackground(String... urls) {
        Log.d(TAG, "doInBackground============");
        Log.d(TAG, "urls[0]:" + urls[0]);
        return "test";
    }

    @Override
    protected void onPostExecute(String result){
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Log.d(TAG, "onPostExecute============");
    }
}


}
于 2012-08-14T00:48:21.643 回答