Think Different

[AsyncTask] the task has already been executed 본문

OS/Android

[AsyncTask] the task has already been executed

TENNESSEE 2014. 9. 16. 22:07

Problem

  1. AsyncTask를 상속받아 클래스를 작성
  2. 해당 클래스의 인스턴스를 받아 외부통신을 시도
  3. "the task has already been executed"라는 오류로그와 함께 강제종료


Solution

  1. AsyncTask를 상속받아 작성된 클래스의 한 인스턴스는 한번만 실행가능
  2. 해당 클래스를 사용할 때마다 새로운 인스턴스를 생성하여 사용


Example

public class Network extends AsyncTask {
     //..
}

public class AsyncTaskTest {
     public void getData() {
          String data = new Network().execute().get();
     }
}


Reference

  • http://stackoverflow.com/questions/6373826/execute-asynctask-several-times