2016-11-13 1 views
0

timed 루프와 함께 textView 업데이트를 만들려고합니다. 나는 그것을 반복 할 때마다 invalidate()를 시도했다. 이 작동하는 것으로 보이지만 타이머가 시작될 때 응용 프로그램이 충돌합니다. 여기Android invalidate()가 textView를 업데이트하지 않습니다.

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 
    Intent intent = getIntent(); 
    textypoo = (TextView)findViewById(R.id.textmcview); 

} 

2의 onclick 방법이다 : 나는 몇 가지 물건을 초기화 곳

다음
private Timer timer; 
private TimerTask timerTask = new TimerTask() { 
    @Override 
    public void run() { 
     thangO += 10; 
     textypoo.setText(Integer.toString(thangO)); 
     textypoo.invalidate(); 
    } 
}; 

public void start() { 
    if (timer != null) { 
     return; 
    } 
    timer = new Timer(); 
    timer.scheduleAtFixedRate(timerTask, 0, 2000); 
} 

가에서 onCreate입니다 :

여기에 루프입니다. 교체

public void sendthestuff(View view) { 
    setContentView(R.layout.activity_second); 
    textypoo = (TextView) findViewById(R.id.textmcview); 
    thangO++; 
    message = Integer.toString(thangO); 
    textypoo.setText(message); 


} 

public void bazinga(View view) { 
    start(); 
} 
+0

UI 스레드 – Saveen

+0

어떻게 할 수있는 실행으로 시도 타이머를 호출 내가 그것을 누를 때마다 텍스트 뷰를 업데이트 한, 다른 내가 그랬어? 죄송합니다. 자바의 Android 측을 처음 접해 보았습니다. – moxide

+0

신경 쓰지 마세요. 여기에 코드를 넣어서 알아 냈습니다. http://stackoverflow.com/questions/12850143/android-basics-running-code-in-the-ui-thread to timer. 도움을 주셔서 감사합니다 (: – moxide

답변

0

:

private TimerTask timerTask = new TimerTask() { 
    @Override 
    public void run() { 
     thangO += 10; 
     textypoo.setText(Integer.toString(thangO)); 
     textypoo.invalidate(); 
    } 
}; 

사람 :

private TimerTask timerTask = new TimerTask() { 
    @Override 
    public void run() { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       thangO += 10; 
       textypoo.setText(Integer.toString(thangO)); 
       textypoo.invalidate(); 
      } 
     }); 
    } 
}; 
관련 문제