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

后端 未结 28 3190
鱼传尺愫
鱼传尺愫 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);
    
    0 讨论(0)
  • 2020-11-21 05:35

    Kotlin Answer

    We have to use UI Thread for the job with true way. We can use UI Thread in Kotlin:

    runOnUiThread(Runnable {
       //TODO: Your job is here..!
    })
    

    @canerkaseler

    0 讨论(0)
  • 2020-11-21 05:37

    This happened to my when I called for an UI change from a doInBackground from Asynctask instead of using onPostExecute.

    Dealing with the UI in onPostExecute solved my problem.

    0 讨论(0)
  • 2020-11-21 05:37

    When using AsyncTask Update the UI in onPostExecute method

        @Override
        protected void onPostExecute(String s) {
       // Update UI here
    
         }
    
    0 讨论(0)
提交回复
热议问题