Activity has leaked window that was originally added

后端 未结 30 3250
野趣味
野趣味 2020-11-21 05:48

What is this error, and why does it happen?

05-17 18:24:57.069: ERROR/WindowManager(18850): Activity com.mypkg.myP has leaked window com.android.internal.pol         


        
30条回答
  •  一整个雨季
    2020-11-21 06:12

    I was having the same problem and found this page, and while my situation was different I called finish from a if block before it defined the alert box.

    So, simply calling dismiss wouldn't work (as it hasn't been made yet) but after reading Alex Volovoy's answer and realizing it was the alert box causing it. I tried to add a return statement right after the finish inside that if block and that fixed the issue.

    I thought once you called finish it stopped everything and finished right there, but it doesn't. It seem to go to the end of the block of code it's in then finishes.

    So, if you want to implement a situation where sometimes it'll finish before doing some code you do gotta put a return statement right after the finish or it'll keep on going and and act like the finish was called at the end of the block of code not where you called it. Which is why I was getting all those weird errors.

    private picked(File aDirectory){
         if(aDirectory.length()==0){
            setResult(RESULT_CANCELED, new Intent()); 
            finish(); 
            return;
        }
         AlertDialog.Builder alert= new AlertDialog.Builder(this); // Start dialog builder
         alert
            .setTitle("Question")
            .setMessage("Do you want to open that file?"+aDirectory.getName());
        alert
            .setPositiveButton("OK", okButtonListener)
            .setNegativeButton("Cancel", cancelButtonListener);
        alert.show();
    }
    

    If you don't put the return right after I called finish in there, it will act as if you have called it after the alert.show(); and hence it would say that the window is leaked by finishing just after you made the dialog appear, even though that's not the case, it still think it is.

    I thought I'd add this as here as this shows the finish command acted differently then I thought it did and I'd guess there are other people who think the same as I did before I discovered this.

提交回复
热议问题