I am making a scientific app that is already close to 45 thousand lines and I use asynchronous tasks in order to be able to interrupt long tasks, if so desired, with a certain click of the user.
Thus, there is a responsive user interface and sometimes a long task in parallel.
When the long task is over, I need to run a routine that manages the user interface.
So, at the end of an asynchronous task, I do a following action that involves the interface, which cannot be performed directly, otherwise it gives an error. So I use
this.runOnUiThread(Runnable { x(...)}) // Kotlin
Many times, this error occurs in some point of function x
.
If function x
was called outside a thread
x(...) // Kotlin
Android Studio
would show a call stack with the error line and one easily could fix the problem in few minutes.
As my source code is tamed, and there is no serious structural problem (many answers above describe this kind of errors), the reason for this scary error message is more gross and less important.
It's just any fool mistake in this execution linked to a thread (like, for example, accessing a vector beyond the defined length), as in this schematic example:
var i = 10 // Kotlin
...
var arr = Array(5){""}
var element = arr[i] // 10 > 5, it's a index overflow
Regarding this stupid error, Android Studio
unfortunately doesn't point to it.
I even consider it a bug, because Android Studio
knows that there is an error, where it is located, but, for some unknown reason, it gets lost and gives a random message, totally disconnected from the problem, i.e, a weird message with no hint showing up.
The solution: Have a lot of patience to run step by step in the debugger until reaching the error line, which Android Studio
refused to provide.
This has happened to me several times and I guess it's an extremely common mistake on Android projects. I had never before given this kind of error to me before I used threads.
Nobody is infallible and we are liable to make small mistakes. In this case, you cannot count on the direct help of Android Studio
to discover where is your error!