How do we use runOnUiThread in Android?

前端 未结 12 780
太阳男子
太阳男子 2020-11-22 00:09

I\'m new to Android and I\'m trying to use the UI-Thread, so I\'ve written a simple test activity. But I think I\'ve misunderstood something, because on clicking the button

12条回答
  •  醉酒成梦
    2020-11-22 00:46

    You have it back-to-front. Your button click results in a call to runOnUiThread(), but this isn't needed, since the click handler is already running on the UI thread. Then, your code in runOnUiThread() is launching a new background thread, where you try to do UI operations, which then fail.

    Instead, just launch the background thread directly from your click handler. Then, wrap the calls to btn.setText() inside a call to runOnUiThread().

提交回复
热议问题