Android “Only the original thread that created a view hierarchy can touch its views.”

后端 未结 28 3302
鱼传尺愫
鱼传尺愫 2020-11-21 04:44

I\'ve built a simple music player in Android. The view for each song contains a SeekBar, implemented like this:

public class Song extends Activity implement         


        
28条回答
  •  醉梦人生
    2020-11-21 05:35

    I was working with a class that did not contain a reference to the context. So it was not possible for me to use runOnUIThread(); I used view.post(); and it was solved.

    timer.scheduleAtFixedRate(new TimerTask() {
    
        @Override
        public void run() {
            final int currentPosition = mediaPlayer.getCurrentPosition();
            audioMessage.seekBar.setProgress(currentPosition / 1000);
            audioMessage.tvPlayDuration.post(new Runnable() {
                @Override
                public void run() {
                    audioMessage.tvPlayDuration.setText(ChatDateTimeFormatter.getDuration(currentPosition));
                }
            });
        }
    }, 0, 1000);
    

提交回复
热议问题