【发布时间】:2011-07-06 22:12:38
【问题描述】:
我在 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);
}
}
这很好用。现在我想要一个计时器来计算歌曲进度的秒数/分钟数。所以我在布局中放了一个TextView,在onCreate() 中用findViewById() 得到它,然后在progress.setProgress(pos) 之后把它放在run() 中:
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() 中触摸它——它并没有给我这个抱怨。
【问题讨论】: