Java. Lang. illegalargumentexception: view not attached to window manager when dismissing Dialog

Source: Internet
Author: User
Tags call back

Exception one,
Java. Lang. illegalargumentexception: view not attached to Window Manager
At Android. View. windowmanagerimpl. findviewlocked (windowmanagerimpl. Java: 664)
At Android. View. windowmanagerimpl. removeview (windowmanagerimpl. Java: 354)
At Android. View. windowmanagerimpl $ compatmodewrapper. removeview (windowmanagerimpl. Java: 165)
At Android. App. Dialog. dismissdialog (dialog. Java: 319)
At Android. App. Dialog. Dismiss (dialog. Java: 302)
At com. Android. Email. activity. setup. accountsetupbasics $ findprovidertask. onpostexecute (accountsetupbasics. Java: 914)
At com. Android. Email. activity. setup. accountsetupbasics $ findprovidertask. onpostexecute (accountsetupbasics. Java: 863)
At Android. OS. asynctask. Finish (asynctask. Java: 631)
At Android. OS. asynctask. Access $600 (asynctask. Java: 177)
At Android. OS. asynctask $ internalhandler. handlemessage (asynctask. Java: 644)
At Android. OS. handler. dispatchmessage (handler. Java: 99)
At Android. OS. lorule. Loop (lorule. Java: 137)
At Android. App. activitythread. Main (activitythread. Java: 4780)
At java. Lang. Reflect. method. invokenative (native method)
At java. Lang. Reflect. method. Invoke (method. Java: 511)
At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 808)
At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 575)
At Dalvik. system. nativestart. Main (native method)

Exception two,
Stack trace:
Java. Lang. illegalargumentexception: view not attached to Window Manager
At Android. View. windowmanagerimpl. findviewlocked (windowmanagerimpl. Java: 355)
At Android. View. windowmanagerimpl. removeview (windowmanagerimpl. Java: 200)
At Android. View. Window $ localwindowmanager. removeview (window. Java: 432)
At Android. App. Dialog. dismissdialog (dialog. Java: 280)
At Android. App. Dialog. Access $000 (dialog. Java: 73)
At Android. App. Dialog $ 1.run( dialog. Java: 109)
At Android. App. Dialog. Dismiss (dialog. Java: 264)
At com. XXXXXXXX. suquashi. actiondialog. Access $001 (actiondialog. Java: 43)
At com. XXXXXXXX. suquashi. actiondialog $2. onanimationend (actiondiashi. Java: 299)
At Android. View. animation. animationset. gettransformation (animationset. Java: 329)
At Android. View. viewgroup. drawchild (viewgroup. Java: 1494)
At Android. View. viewgroup. dispatchdraw (viewgroup. Java: 1356)
At Android. View. viewgroup. drawchild (viewgroup. Java: 1627)
At Android. View. viewgroup. dispatchdraw (viewgroup. Java: 1356)
At Android. View. viewgroup. drawchild (viewgroup. Java: 1627)

[Bad design]
The following code is a bad design to put dialog. Dismiss () and activity. Finish () in the asynctask.

Final asynctask <myparams, object, myresult> task = new asynctask <myparams, object, myresult> () {@ override protected myresult doinbackground (myparams... params) {// long operation goes here} @ override protected void onpostexecute (myresult result) {dialog. dismiss (); oncompletion (result); finish ();}};

First every dialog or something extends dialog wowould have a own window and d é cor view (itself), and then t popup (DIALOG) there is a window and d é cor view. second when the phone change orientation, the activity thread wocould call handlerelaunchactivity (this wocould call handledestroyactivity ), the Window Manager wocould remove all the window and d é cor view and then create new window and d é cor, so the old d é cor has no use after this had been done.
In the code of action dialog there wocould be a animation make the super dismiss called later, but if the handlerelaunchactivity happened, the actiondialog's d écor wocould be useless, then the animation call back come, the super dismiss is called, it shocould not be in the mview [] In windowmanagerimpl. after that analyze, I think if this happened, we don't have very useful method to prevent, and it shoshould be OK for a not in the array, but if lockview method throw a exception, I Think We coshould just catch it. it won't affect much because the D écor view (actiondialog) wocould be useless.

Change affects the way the actiondialog behaves when it is dismissed. Most notable for an end user is that an actiondialog that is closed when its activity is being shut down will no longer animate.

Addition to the information below the reason for the dialog being removed from windowmanager in the first place is typically due to a rotation of the screen, and if the actiondialog is managed the Android system will take care of removing/dismissing it.

The problem seems to be because actiondialog delays dialog. dismiss () to play an animation. when the animation is finished actiondialog calldialog. dismiss but at that time it has already been removed from the windowmanager so the windowmanager will throw an exception when dialog requests to be removed from windowmanager.

This problem is thus related to actiondialog and not homescreen which means that other applications using actiondialog cocould also get this problem. I have seen one MTBF-crash for com. sonyericsson. contacts and one for timescape due to this.

it cocould be wrong here, but I suspect that multiple phones 'in the wild 'have a bug that causes them to switch orientation on applications that are marked as statically oriented. this happens quite a bit on my personal phone, and on login of the test phones our group uses (including droid, N1, G1, Hero ). typically an app marked as statically oriented (perhaps vertically) will lay itself out for a second or two using a horizontal orientation, and then immediately switch back. end result is that even though you don't want your app to switch orientation, you have to be prepared that it may. I don't know under what exact conditions this behavior can be reproduced, I don't know if it is specific to a version of Android. all I know is that I have seen it happen plenty of times

I wocould recommend using the solution provided in the link you posted that suggests overriding the activity oncreatedialog method and letting the Android OS manage the lifecycle of your dialogs. it looks to me like even though you don't want your activity to switch orientations, It is switching orientation somewhere. you can try to track down a method that will always prevent orientation switching, but I am trying to tell you that I personally don't believe there is a foolproof way that works on all current Android phones in the market.

[Dialog dismiss framework code]

 
301 public void dismiss () {302 if (thread. currentthread ()! = Muithread) {303 mhandler. Post (mdismissaction); 304} else {305 mhandler. removecallbacks (mdismissaction); 306 mdismissaction. Run (); 307} 308}

for exception one, in email, illegalargumentexception rises in onpostexec when the asynctsk is finishing.
when dismissing the dialog, windowmanager can't find the window and view in its views.

300 public void dismiss () {301 if (logoff. mylofter () = mhandler. getlooper () {302 dismissdialog (); 303} else {304 mhandler. post (mdismissaction); 305} 306} 307308 void dismissdialog () {309 if (mdecor = NULL |! Mshowing) {310 return; 311} 312313 if (mwindow. isdestroyed () {314 log. E (TAG, "tried to dismissdialog () but the dialog's window was already destroyed! "); 315 return; 316} 317318 try {319 mwindowmanager. removeview (mdecor); 320} finally {321 If (mactionmode! = NULL) {322 mactionmode. finish (); 323} 324 mdecor = NULL; 325 mwindow. closeallpanels (); 326 onstop (); 327 mshowing = false; 328329 senddismissmessage (); 330} 331} 158 @ override159 public void removeview (view) {160 if (null = view) 161 {162 log. D ("windowmanager", "uunote: removeview: view = NULL ###"); 163 throw new nullpointerexception ("view parameter is null into findviewlocked ()"); 164} 165 mwindowmanage R. removeview (View); 166} 352 public void removeview (view) {353 synchronized (this) {354 int Index = findviewlocked (view, true ); 355 view curview = removeviewlocked (INDEX); 356 if (curview = view) {357 return; 358} 359360 throw new illegalstateexception ("calling with View" + view361 + "but the viewancestor is attached to" + curview); 362} 363 private int findviewlocked (view, boolean R Equired) {650 synchronized (this) {651 final int COUNT = mviews! = NULL? Mviews. length: 0; 652 for (INT I = 0; I <count; I ++) {653 if (mviews [I] = view) {654 return I; 655} 656} 657 if (null = view) 658 {659 log. D ("windowmanager", "findviewlocked: view = NULL ###"); 660 required = false; 661 throw new nullpointerexception ("view parameter is null into findviewlocked ()"); 662} 663 if (required) {664 throw new illegalargumentexception (665 "view not attached to Window Manager"); 666} 667 return-1; 668} 669} 670}

[Reference solution]
Move the dialog. Dismiss () and activity. Finish to uithread.

 
New thread () {2946 @ override 2947 public void run () {2948 uri = contenturis. withappendedid (semcthreads. content_uri, 2949 threadid); 2950 getcontentresolver (). delete (Uri, null, null); 2951 runonuithread (New runnable () {2952 public void run () {2953 PD. dismiss (); 2954 2955 // Prevent saving of draft 2956 mcomposer = NULL; 2957 mconversation = NULL; 2958 2959 notifications. updateall (conversationactivity. this); 2960 conversationcache. clear (); 2961 finish (); 2962} 2963}); 2964} 2965 }. start ();

If you use your dialogs, shocould close all dialog in activity's overridenOndestroy () or onstop ().
Then you may be able to reduce the frequency 'java. Lang. illegalargumentexception: view not attached to window manager' exception occurs.

 
@ Overrideprotected void ondestroy () {log. D (TAG, "called ondestroy"); mdialog. Dismiss (); Super. ondestroy ();}

But little exceed...
To make it more clear, you prevent to show any dialog after ondestroy called.
I don't use as below. But it's clear.

Private Boolean misdestroyed = false; private void showdialog () {closedialog (); If (misdestroyed) {log. D (TAG, "called ondestroy () already. "); return;} mdialog = new alertdialog (this ). settitle ("title "). setmessage ("this is dialogtest "). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {dialog. dismiss ();}}). create (); MDI Alog. Show ();} private void closedialog () {If (mdialog! = NULL & mdialog. isshowing () {mdialog. dismiss () ;}@ overrideprotected void ondestroy () {log. D (TAG, "called ondestroy"); misdestroyed = true; closedialog (); super. ondestroy ();}

And at last, is it better to use dialog. Cancel () rather than dialog. Dismiss ()?
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.