Android 无法使用处理程序每秒更新TextView

Android 无法使用处理程序每秒更新TextView,android,Android,我正在尝试每秒更新一次TextView。特别是它是一首歌的计时器。我正在使用一个处理程序来实现这一点。它似乎设置正确-我没有任何异常,调试时也没有看到任何明显的错误-但它悄悄地无法更新TextView。它只停留在0点 private TextView currentTime; private int startTime = 0; private Handler timeHandler = new Handler(); private Runnable updateTime = new Runna

我正在尝试每秒更新一次TextView。特别是它是一首歌的计时器。我正在使用一个处理程序来实现这一点。它似乎设置正确-我没有任何异常,调试时也没有看到任何明显的错误-但它悄悄地无法更新TextView。它只停留在0点

private TextView currentTime;
private int startTime = 0;
private Handler timeHandler = new Handler();
private Runnable updateTime = new Runnable() {
    public void run() {
        final int start = startTime;
        int millis = appService.getSongPosition() - start;
        int seconds = (int) ((millis / 1000) % 60);
        int minutes = (int) ((millis / 1000) / 60);
        Log.d("seconds",Integer.toString(seconds)); // looks okay, prints new value every second
        if (seconds < 10) {
            // this is hit, yet the textview is never updated
            currentTime.setText(String.format("%d:0%d",minutes,seconds));
        } else {
            currentTime.setText(String.format("%d:%d",minutes,seconds));
        }
        timeHandler.postAtTime(this,start+(((minutes*60)+seconds+1)*1000));
    }
};

private ServiceConnection onService = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
        IBinder rawBinder) {
    appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer

    // start playing the song, etc...

    if (startTime == 0) {
        startTime = appService.getSongPosition();
        timeHandler.removeCallbacks(updateTime);
        timeHandler.postDelayed(updateTime,1000);
    }
}

public void onServiceDisconnected(ComponentName classname) {
    appService = null;
}
};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.song);

    currentTime = (TextView) findViewById(R.id.current_time);

    // ...

    bindIntent = new Intent(Song.this,MPService.class);
    bindService(bindIntent,onService,0);
}
私有文本查看当前时间;
私有int startTime=0;
私有处理程序timeHandler=新处理程序();
private Runnable updateTime=new Runnable(){
公开募捐{
最终整数开始=开始时间;
int millis=appService.getSongPosition()-start;
整数秒=(整数)((毫秒/1000)%60);
整数分钟=(整数)((毫秒/1000)/60);
Log.d(“秒”,Integer.toString(秒));//看起来不错,每秒打印一个新值
如果(秒<10){
//这是命中的,但文本视图从未更新
currentTime.setText(String.format(“%d:0%d”,分,秒));
}否则{
currentTime.setText(String.format(“%d:%d”,分,秒));
}
timeHandler.posttime(这个,开始+((分钟*60)+秒+1)*1000));
}
};
private ServiceConnection onService=新ServiceConnection(){
ServiceConnected上的公共无效(ComponentName类名称,
伊宾德(拉文德){
appService=((MPService.LocalBinder)rawBinder.getService();//处理MediaPlayer的服务
//开始播放歌曲等。。。
如果(开始时间==0){
startTime=appService.getSongPosition();
removeCallbacks(updateTime);
timeHandler.postDelayed(updateTime,1000);
}
}
ServiceDisconnected上的公共void(ComponentName类名称){
appService=null;
}
};
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
currentTime=(TextView)findViewById(R.id.current\u time);
// ...
bindIntent=新意图(Song.this,MPService.class);
bindService(bindIntent,onService,0);
}
有什么想法吗?

解决了:

解决了: