Skip to content Skip to sidebar Skip to footer

Window Leak Due To Alert Dialog

Quite new in Android development, I keep getting a window leak exception, probably due to use of an alertbox I made. In searching for what I have to change, I tried a bunch of thin

Solution 1:

The window leak is not the root cause of your crash; the primary problem here is that you're trying to create a dialog from a background thread:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

In AlertBox, you're creating a new dialog, which in turn contain their own Handler:

new AlertDialog.Builder(context)
          [...].show();

However, you're creating AlertBox from your AsyncTask's doInBackground():

newAlertBox(context, "Error", e.toString());

Move that logic into the main thread, for instance by handling the error in onPostExecute().

Secondarily, you're getting the "window leaked" message because the AsyncTask is still running while the activity is gone, and it contains references to objects attached to that window, specifically pDialog.

Post a Comment for "Window Leak Due To Alert Dialog"