2012-04-13 2 views
0

은 내가 이런 코드가 안드로이드에서의 TimerTask에 문제가 : 내가 텍스트 뷰에 접근하고 있습니다 때문에 타이머 작업을 얻을 startet 내 응용 프로그램이 충돌안드로이드의 새로운 TimerTask 액션에서 뭔가를 바꾸는 방법?

timer = new Timer(); 
timer.schedule(new TimerTask() { 
     public void run() { 
      countInt = countInt + 1; 
      textview1.setText(countInt); 
     } 
    }, 1000); 

때마다, 나는 일을하고 그것은이다 다른 스레드가 맞습니까?

어떻게 해결할 수 있습니까?

답변

3

이 시도 ..

timer = new Timer(); 
    timer.schedule(new TimerTask() { 
      public void run() { 
       countInt = countInt + 1; 
       yourActivity.this.runOnUiThread(new Runnable() 
       public void run(){ 
        {textview1.setText(String.valueOf(countInt))}); 
       } 
      } 
     }, 1000); 

이 허용되지 않습니다 UI 스레드에 속한 뭔가 (textview1.setText(countInt);) 덤비는되어 있기 때문에 충돌이 ...

4

예, 당신은이 원인을 '당신이 바로 충돌하는 UI 스레드가 아닌보기에서 액세스하고 있습니다. 이를 해결하기 위해 귀하의 활동을 사용하여 Runnable to UI 스레드를 게시 할 수 있습니다.

timer = new Timer(); 
timer.schedule(new TimerTask() { 
    public void run() { 
     countInt = countInt + 1; 
     YourActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       textview1.setText(countInt); 
      } 
     }); 
    } 
}, 1000); 
관련 문제