Activity has leaked window that was originally added

后端 未结 30 3292
野趣味
野趣味 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:21

    You can get this exception by just a simple/dumb mistake, by (for example) accidentally calling finish() after having displayed an AlertDialog, if you miss a break call statement in a switch statement...

       @Override
       public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_button:
                openMyAlertDialog();
                break; <-- If you forget this the finish() method below 
                           will be called while the dialog is showing!
            case R.id.exit_button:
                finish();
                break;
            }
        }
    

    The finish() method will close the Activity, but the AlertDialog is still displaying!

    So when you're staring intently at the code, looking for bad threading issues or complex coding and such, don't lose sight of the forest for the trees. Sometimes it can be just something as simple and dumb as a missing break statement. :)

提交回复
热议问题