The question is: should I call Activity.findViewById
in the UI thread only?
The reason I'm asking is that I call it from an arbitrary thread and it works flawlessly.
But then I got an exception from one of the users: findViewById
fails with NPE
somewhere in findViewTraversal
.
So, can it be the cause?
the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread.
Taken from Painless threading.
Because it's not thread-safe, you don't know what will happen if you use the API in another thread. It might work, it might not work, it might work most of the time, might never work or it might work and breaks other things. You can't tell.
Thus, if you want a correct application, you must do this in the UI thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With