我已经在Android中构建了一个简单的音乐播放器。每首歌曲的视图都包含一个SeekBar,实现如下:
public class Song extends Activity implements OnClickListener,Runnable {
    private SeekBar progress;
    private MediaPlayer mp;
    // ...
    private ServiceConnection onService = new ServiceConnection() {
          public void onServiceConnected(ComponentName className,
            IBinder rawBinder) {
              appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
              progress.setVisibility(SeekBar.VISIBLE);
              progress.setProgress(0);
              mp = appService.getMP();
              appService.playSong(title);
              progress.setMax(mp.getDuration());
              new Thread(Song.this).start();
          }
          public void onServiceDisconnected(ComponentName classname) {
              appService = null;
          }
    };
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song);
        // ...
        progress = (SeekBar) findViewById(R.id.progress);
        // ...
    }
    public void run() {
    int pos = 0;
    int total = mp.getDuration();
    while (mp != null && pos<total) {
        try {
            Thread.sleep(1000);
            pos = appService.getSongPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }
        progress.setProgress(pos);
    }
}这很好。现在,我需要一个计时器来计算歌曲进度的秒/分钟。因此,我将a TextView放入布局findViewById()中onCreate(),并将其放入in 中,然后将其放入run()之后progress.setProgress(pos):
String time = String.format("%d:%d",
            TimeUnit.MILLISECONDS.toMinutes(pos),
            TimeUnit.MILLISECONDS.toSeconds(pos),
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(
                    pos))
            );
currentTime.setText(time);  // currentTime = (TextView) findViewById(R.id.current_time);但是最后一行给我一个例外:
android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
但是,我在这里做的基本上与在SeekBar-中创建视图onCreate,然后在中触摸它run()-一样,并且不会给我这种抱怨。

error.setText(res.toString());在run()方法中做一个内部操作,但是我不能使用res,因为它不是最终的。.太糟糕了