Kotlin app using recreate() function Error:"java.lang.IllegalStateException: Must be called from main thread"

95 Views Asked by At

I get the error"java.lang.IllegalStateException: Must be called from main thread" when using the "recreate()" function to refresh my activity in Kotlin-Android Studio.

What is causing the error?

Edit: I solved the problem by running the following instead: " runOnUiThread { recreate() } ", but I still do not understand why I get the error.

1

There are 1 best solutions below

0
On

Apparently, you were invoking recreate() from a thread other than the UI thread.

You didn't show your code, so we can't tell how you were on this other thread.

All UI operations have to be done on the UI, or "main" thread. Therefore the error.