android kotlin 子线程中调用界面UI组件崩溃


UI 只能在主线程内更新,子线程需要更新UI组件时可以这样:

    fun fuck(){
        Executors.newSingleThreadExecutor().execute{
            // url request ...
            val visibility = View.VISIBLE

            Handler(mainLooper).post {
                val btn = findViewById<Button>(R.id.button)
                btn.visibility = visibility
            }
        }

        Thread{
            println("hi there")
            Handler(Looper.getMainLooper()).post{
                  val btn = findViewById<Button>(R.id.button)
            }
        }.start()
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM