Activity has leaked window that was originally added

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

    Not only try to show an alert but it can also be invoked when you finish a particular instance of activity and try to start new activity/service or try to stop it.

    Example:

    OldActivity instance;
    
        oncreate() {
           instance=this;
        }
        instance.finish();
        instance.startActivity(new Intent(ACTION_MAIN).setClass(instance, NewActivity.class));
    
    0 讨论(0)
  • 2020-11-21 06:28

    This problem arises when trying to show a Dialog after you've exited an Activity.

    I just solved this problem just by writing down the following code:

    @Override
    public void onDestroy(){
        super.onDestroy();
        if ( progressDialog!=null && progressDialog.isShowing() ){
            progressDialog.cancel();
        }
    }
    

    Basically, from which class you started progressDialog, override onDestroy method and do this way. It solved "Activity has leaked window" problem.

    0 讨论(0)
  • 2020-11-21 06:28

    The "Activity has leaked window that was originally added..." error occurs when you try show an alert after the Activity is effectively finished.

    You have two options AFAIK:

    1. Rethink the login of your alert: call dismiss() on the dialog before actually exiting your activity.
    2. Put the dialog in a different thread and run it on that thread (independent of the current activity).
    0 讨论(0)
  • 2020-11-21 06:28

    In my case, the reason was that I forgot to include a permission in the Android manifest file.

    How did I find out? Well, just like @Bobby says in a comment beneath the accepted answer, just scroll further up to your logs and you'll see the first reason or event that really threw the Exception. Apparently, the message "Activity has leaked window that was originally added" is only an Exception that resulted from whatever the first Exception is.

    0 讨论(0)
  • 2020-11-21 06:29

    This can be if you have an error at doInBackground() function and have this code.

    Try to add dialog at last. At first check and fix doInBackground() function

    protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(CreateAccount.this);
         pDialog.setMessage("Creating Product..");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(true);
         pDialog.show();
    
     }
    
     protected String doInBackground(String...args) {
         ERROR CAN BE IS HERE
     }
    
     protected void onPostExecute(String file_url) {
         // dismiss the dialog once done
         pDialog.dismiss();
    
    0 讨论(0)
  • 2020-11-21 06:31

    Try below code , it will work any time you will dismiss the progress dialogue and it will see whether its instance is available or not.

    try {
            if (null != progressDialog && progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
提交回复
热议问题