我在Observer.update
內嘗試setText
TextView
時遇到問題。我試圖調試,它確實運行到更新功能中,但setText
調用被忽略。無法更新Observer.update內的TextView
if (step.getMyTimer().countObservers() == 0){
Observer ob = new Observer() {
@Override
public void update(Observable observable, Object data) {
timerTextView.setText(step.getMyTimer().toString());
Log.v("INSIDE OBSERVER ", "");
}
};
step.getMyTimer().addObserver(ob);
Log.v("ADD OBSERVER :", "");
}
在logcat中,它沒有打印出INSIDE OBSERVER
。 有人有什麼想法嗎?
於是,我就加入到Observer.update
和定時器這樣的:
Observer ob = new Observer() {
@Override
public void update(Observable observable, Object data) {
Log.v("THREAD UPDATE OBSERVER ", ""+Looper.myLooper().getThread().getName());
}
};
step.getMyTimer().addObserver(ob);
Log.v("THREAD ADD OBSERVER :", "" + Looper.myLooper().getThread().getName());
在計時器:
_timer = new CountDownTimer(min * 60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tick();
setChanged();
notifyObservers();
clearChanged();
Log.v("THREAD TIMER : ", "" + Looper.myLooper().getThread().getName());
}
...
}
這是輸出:
09-17 15:09:32.312 V/MAIN THREAD﹕ main--main
09-17 15:09:32.544 V/THREAD ADD OBSERVER :﹕ main
09-17 15:09:32.554 V/THREAD ADD OBSERVER :﹕ main
09-17 15:09:38.703 V/THREAD UPDATE OBSERVER﹕ main
09-17 15:09:38.703 V/THREAD TIMER :﹕ main
09-17 15:09:39.705 V/THREAD UPDATE OBSERVER﹕ main
09-17 15:09:39.705 V/THREAD TIMER :﹕ main
09-17 15:09:40.706 V/THREAD UPDATE OBSERVER﹕ main
09-17 15:09:40.706 V/THREAD TIMER :﹕ main
09-17 15:09:41.707 V/THREAD UPDATE OBSERVER﹕ main
09-17 15:09:41.707 V/THREAD TIMER :﹕ main
嘗試登錄'step.getMyTimer()。的toString()'值和檢查日誌 –
什麼讓step.getMyTimer的'內容() .toString()'確實發生了變化,但仍不能'setText' –
如何聲明timerTextView並在xml中定義佈局? – headuck