2012-06-27 2 views
2

Android 용 Eclipse를 사용하고 있습니다. 짧은 지연이있는 간단한 반복 타이머를 만들려고합니다. TextView timerTV를 클릭하면 시작됩니다. 내가하는 "클래스 파일 편집기"를 실행하려고실제로 작동하는 Timer.scheduleAtFixedRate()를 어떻게 설정합니까?

timerTV = (TextView) findViewById(R.id.timerTV); 
    timerTV.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

       Timer gameTimer = new Timer(); 
       TimerTask doThis; 

       int delay = 5000; // delay for 5 sec. 
       int period = 1000; // repeat every sec. 
       doThis = new TimerTask() { 
       public void run() { 
           Toast.makeText(getApplicationContext(), "timer is running", Toast.LENGTH_SHORT).show(); 
       } 
       }; 
       gameTimer.scheduleAtFixedRate(doThis, delay, period); 

매번 오류와 함께 팝업 : \ 프로그램 파일 \ JAR 파일의 C "소스를 찾을 수 없습니다"이 코드에서 onCreate 방법에 Android \ android-sdk \ platforms \ android-8 \ android.jar에는 소스 첨부 파일이 없습니다. 아래의 소스 첨부를 클릭하여 소스를 첨부 할 수 있습니다 : [첨부 소스 ...] 이것을 클릭하면 'android.jar'가 포함 된 위치 폴더를 선택하라는 메시지가 나타납니다. 이 작업을 시도했지만 탐색 할 수 없습니다 어쨌든 폴더에있는 모든 방법.

문제는 내 코드 어딘가에 있다고 가정합니다. 몇 시간 동안 검색하고 코드를 여러 번 붙여 넣었습니다.

답변

5

runOnUiThread()와 함께 실제 Timer (java.util.Timer)를 사용하면이 문제를 해결할 수있는 방법 중 하나이며이를 구현하는 방법의 예는 다음과 같습니다.

public class myActivity extends Activity { 

private Timer myTimer; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    myTimer = new Timer(); 
    myTimer.schedule(new TimerTask() { 
     @Override 
     public void run() { 
      TimerMethod(); 
     } 

    }, 0, 1000); 
} 

private void TimerMethod() 
{ 
    //This method is called directly by the timer 
    //and runs in the same thread as the timer. 

    //We call the method that will work with the UI 
    //through the runOnUiThread method. 
    this.runOnUiThread(Timer_Tick); 
} 

private Runnable Timer_Tick = new Runnable() { 
    public void run() { 

    //This method runs in the same thread as the UI.    

    //Do something to the UI thread here 

    } 
}; 
} 

자료 제공 : http://steve.odyfamily.com/?p=12

+2

좋은 이유는 어떤 방법을 활용하지 않는있다 생성자 및 변수 이름 이외. – lhunath

0

프로젝트 -> 정리를 사용하여 프로젝트를 마우스 오른쪽 단추로 클릭하고 프로젝트 속성 수정을 찾습니다. 빌드 경로를 확인하십시오. 이 모든 것들 중 하나 일 수 있습니다. Eclipse를 다시 시작하고 Android Manifest가 올바른 API를 타겟팅하는지 확인하십시오.

+0

나는 아무 소용이 시도했지만 여기에 해결책을 발견 : http://steve.odyfamily.com/?p=12 –

관련 문제