Android - rare runtime exception that does not point to any class where it starts

android

Solution

In some devices and also some cases activity gets finished before dialog successfully dismisses or asynchronous task is running in background , so when you call the dialoge to dismiss it causes

`java.lang.IllegalArgumentException: View not attached to window manager`

as the main ui thread is already dismissed.

In order to solve this you can track if the activity finished , so make the dialog null in onStop of the activity and then in onPostExecute check the status of the dialog; Code will be like

@Override
public void onStop() {
    super.onStop();
    dialog = null;
}

and in onpostexecute

if (dialog != null) { 
        dialog.dismiss();
   }

Problem

I am getting this runtime exception about once every thousand+ sessions on the app, so it is rare and I have not been able to reproduce it. ``` java.lang.IllegalArgumentException: View not attached to window manager at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:381) at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:226) at android.view.Window$LocalWindowManager.removeView(Window.java:432) at android.app.Dialog.dismissDialog(Dialog.java:278) at android.app.Dialog.access$000(Dialog.java:71) at android.app.Dialog$1.run(Dialog.java:111) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3691) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) at dalvik.system.NativeStart.main(Native Method) ``` Would anyone know how to reproduce it, or why it happens? Or better yet, how to fix it? :) Thanks! EDIT: I added the suggestion made by Raghav to add this: ``` <activity android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:name="ActivityName"> ``` But this has not fixed the crashing and the crashing is still happening. EDIT: Here is some typical code I use: ``` public class ProblemActivity extends BaseListActivity { Dialog dialog; .... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // After some action dialog.show(); } public class GetSolutionTopicsTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... theParams) { String myUrl = theParams[0]; String problem_id = theParams[1]; String charset = "UTF-8"; String response = null; try { String query = String.format("problem_id=%s", URLEncoder.encode(problem_id, charset)); final URL url = new URL( myUrl + "?" + query ); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setUseCaches(false); conn.connect(); final InputStream is = conn.getInputStream(); final byte[] buffer = new byte[8196]; int readCount; final StringBuilder builder = new StringBuilder(); while ((readCount = is.read(buffer)) > -1) { builder.append(new String(buffer, 0, readCount)); } response = builder.toString(); } catch (Exception e) { } return response; } @Override protected void onPostExecute(String result) { if ( result == null ) { try { dialog.dismiss(); } catch (Exception e) { // nothing } } else { try { dialog.dismiss(); } catch (Exception e) { // nothing } } // End of else } } ```

Original source

Related problems