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

后端 未结 28 3300
鱼传尺愫
鱼传尺愫 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:10

    For the people struggling in Kotlin, it works like this:

    lateinit var runnable: Runnable //global variable
    
     runOnUiThread { //Lambda
                runnable = Runnable {
    
                    //do something here
    
                    runDelayedHandler(5000)
                }
            }
    
            runnable.run()
    
     //you need to keep the handler outside the runnable body to work in kotlin
     fun runDelayedHandler(timeToWait: Long) {
    
            //Keep it running
            val handler = Handler()
            handler.postDelayed(runnable, timeToWait)
        }
    

提交回复
热议问题