我在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() { …Run Code Online (Sandbox Code Playgroud) 在我的Android应用程序中,我正在使用Timer schedule.but得到Timer-0致命异常如下所示.我可以删除它.我还提到下面的代码 -
01-28 13:44:41.142: E/AndroidRuntime(1307): FATAL EXCEPTION: Timer-0
01-28 13:44:41.142: E/AndroidRuntime(1307):android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.View.invalidate(View.java:5279)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.View.setBackgroundDrawable(View.java:7626)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.View.setBackgroundResource(View.java:7535)
01-28 13:44:41.142: E/AndroidRuntime(1307): at com.example.iqtest.PlayGame$1.run(PlayGame.java:61)
01-28 13:44:41.142: E/AndroidRuntime(1307): at java.util.Timer$TimerImpl.run(Timer.java:284)
Run Code Online (Sandbox Code Playgroud)
和代码是 -
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated …Run Code Online (Sandbox Code Playgroud)