Android is a rare run-time exception that does not point to any class where it runs

I get this exception at runtime approximately every thousand + sessions in the application, so this is rare and I could not 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) 

Does anyone know how to reproduce it, or why is this happening? Or better yet, how to fix it? :)

Thanks!

EDIT:

I added a Raghava sentence to add this:

 <activity android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:name="ActivityName"> 

But this did not fix the failure, and the failure 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 } } 
+4
source share
7 answers

In some devices, as well as in some cases, the activity ends before the dialog is successfully rejected or the asynchronous task is running in the background, so when you call the dialog to reject it, it calls

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

since the main thread ui is already rejected.

To solve this problem, you can track the completion of the action, so make the dialogue null in the onStop action, and then in onPostExecute check the status of the dialog; The code will be like

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

and onpostexecute

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

ConfigChanges does not cause this problem.

From the above logs, it seems that the dialog box displayed in the activity was rejected after the activity is no longer visible.

If you share the code, or at least give the steps that you follow to get the crash, I can suggest a fix.

+4
source

When you switch orientations, Android will create a new view. You are probably getting crashes because your background thread is trying to change state to the old one. (This may also have problems because your background thread is not in the user interface thread)

In your particular case, it seems that you are trying to reject the dialog when this happens, possibly from AsyncTask or the background thread.

To fix the error, try the following:

 <activity android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:name="ActivityName"> 

The fact is that the system destroys the action when a configuration change occurs. See ConfigurationChanges .

Thus, if you delete the system in the configuration file to destroy your activity. Instead, it calls the onConfigurationChanged method.

There is also a similar and older question here , with more solutions. My current answer is a mixture of two answers from there.

+2
source

Are you AsyncTask , which may terminate after the activity is destroyed? Android View not connected to the window manager has several answers.

Also, if your application targets API 13 or higher , you should use this in your manifest instead of the Raghav clause (resizing the screen with orientation in the new APIs):

  <activity android:label="@string/app_name" android:configChanges="orientation|keyboardHidden|screenSize" android:name="ActivityName"> 
+2
source
 private class LongOperation extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { String response = ""; for(int i=0;i<5;i++) { try { response =i; Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } return response; } @Override protected void onPostExecute(String result) { TextView txt = (TextView) findViewById(R.id.output); txt.setText(result); } @Override protected void onPreExecute() { } @Override protected void onProgressUpdate(Void... values) { } 

}

Change it with the code. This is a version issue. After the third version, we do not set the view in the main thread, so we need to use the async task ...

+2
source

The onStop action dimiss dialog box. In addition, remove the check before calling

dialog.isShowing() true or not.

if(dialog.isShowing())dialog.dismiss();

From source code

 private int findViewLocked(View view, boolean required){ synchronized (this){ final int count = mViews != null ? mViews.length : 0; for (int i=0; i<count; i++) { if (mViews[i] == view) { return i; } } if (required) { throw new IllegalArgumentException( "View not attached to window manager"); } return -1; } } 

Since you get this exception, it means that the view was not found when the dismissal was called.

+2
source

I am quite sure that this is the Dialogue that caused this problem, as Chinmoy Debnat pointed out.

+2
source

Source: https://habr.com/ru/post/1442526/


All Articles