[android] Activity has leaked window that was originally added

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!

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to memory-leaks

AngularJS - Does $destroy remove event listeners? Implementing IDisposable correctly Best way to increase heap size in catalina.bat file If a DOM Element is removed, are its listeners also removed from memory? Resource leak: 'in' is never closed android activity has leaked window com.android.internal.policy.impl.phonewindow$decorview Issue This Handler class should be static or leaks might occur: IncomingHandler possible EventEmitter memory leak detected performSelector may cause a leak because its selector is unknown How can I create a memory leak in Java?

Examples related to dialog

Disable click outside of angular material dialog area to close the dialog (With Angular Version 4.0+) How to change DatePicker dialog color for Android 5.0 Android simple alert dialog Swift alert view with OK and Cancel: which button tapped? How to make a edittext box in a dialog How to check if activity is in foreground or in visible background? jquery ui Dialog: cannot call methods on dialog prior to initialization JavaScript: Create and save file Prevent Android activity dialog from closing on outside touch How to use OpenFileDialog to select a folder?