android activity has leaked window com.android.internal.policy.impl.phonewindow$decorview Issue

133,497

Solution 1

Thank you Guys to give me many suggestions. Finally I got a solution. That is i have started the NetErrorPage intent two times. One time, i have checked the net connection availability and started the intent in page started event. second time, if the page has error, then i have started the intent in OnReceivedError event. So the first time dialog is not closed, before that the second dialog is called. So that i got a error.

Reason for the Error: I have called the showInfoMessageDialog method two times before closing the first one.

Now I have removed the second call and Cleared error :-).

Solution 2

Change this dialog.cancel(); to dialog.dismiss();

The solution is to call dismiss() on the Dialog you created in NetErrorPage.java:114 before exiting the Activity, e.g. in onPause().

Views have a reference to their parent Context (taken from constructor argument). If you leave an Activity without destroying Dialogs and other dynamically created Views, they still hold this reference to your Activity (if you created with this as Context: like new ProgressDialog(this)), so it cannot be collected by the GC, causing a memory leak.

Solution 3

In my case finish() executed immediately after a dialog has shown.

Solution 4

Please try this Way And Let me know :

Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.neterrorlayout);

   mContext=NetErrorPage.this;
   Button reload=(Button)findViewById(R.id.btnReload);
   reload.setOnClickListener(this);    
   showInfoMessageDialog("Please check your network connection","Network Alert"); 
}
public void showInfoMessageDialog(String message,String title)
{
   new AlertDialog.Builder(mContext)
   .setTitle("Network Alert");
   .setMessage(message);
   .setButton("OK",new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,int which) 
       {   
          dialog.cancel();
       }
   })
   .show();
}

Solution 5

The dialog needs to be started only after the window states of the Activity are initialized This happens only after onresume.

So call

runOnUIthread(new Runnable(){

    showInfoMessageDialog("Please check your network connection","Network Alert");
});

in your OnResume function. Do not create dialogs in OnCreate
Edit:

use this

Handler h = new Handler();

h.postDelayed(new Runnable(){

        showInfoMessageDialog("Please check your network connection","Network Alert");
    },500);

in your Onresume instead of showonuithread

Share:
133,497
Ponmalar
Author by

Ponmalar

Updated on May 20, 2020

Comments

  • Ponmalar
    Ponmalar about 4 years

    I am working with Android application to show network error.

    NetErrorPage.java

    package exp.app;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class NetErrorPage extends Activity implements OnClickListener {    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.neterrorlayout);
            Button reload=(Button)findViewById(R.id.btnReload);
            reload.setOnClickListener(this);    
            showInfoMessageDialog("Please check your network connection","Network Alert"); 
        }
    
        public void onClick(View arg0)             
            {
                if(isNetworkAvailable())
                {                   
                    Intent myIntent = new Intent((Activity)NetErrorPage.this, MainActivity.class);   
                    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);              
                    ((Activity)NetErrorPage.this).startActivity(myIntent);
                    finish();
                }
                else
                    showInfoMessageDialog("Please check your network connection","Network Alert");
        }
    
        public void showInfoMessageDialog(String message,String title)
           {
            AlertDialog alertDialog = new AlertDialog.Builder(NetErrorPage.this).create();
            alertDialog.setTitle("Network Alert");
            alertDialog.setMessage(message);
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int which) 
                        {   
                            dialog.cancel();
                        }
                    });            
            alertDialog.show();
        }
    
     private boolean isNetworkAvailable()
        {
            NetworkInfo ActiveNetwork;
            @SuppressWarnings("unused")
            String IsNetworkConnected;
            @SuppressWarnings("unused")
            String ConnectionType;
            ConnectivityManager connectivitymanager;
            connectivitymanager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);        
            try
            {           
                ActiveNetwork=connectivitymanager.getActiveNetworkInfo();
                ConnectionType=ActiveNetwork.getTypeName(); 
                IsNetworkConnected=String.valueOf(ActiveNetwork.getState());
                return true;                        
            }
            catch(Exception error)
            {
                    return false;
            }
        }    
    }
    

    but i'm getting the error as below,

    08-17 11:59:08.019: E/WindowManager(16460): Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here
    08-17 11:59:08.019: E/WindowManager(16460): android.view.WindowLeaked: Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here
    08-17 11:59:08.019: E/WindowManager(16460):     at android.view.ViewRoot.<init>(ViewRoot.java:263)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.Dialog.show(Dialog.java:241)
    08-17 11:59:08.019: E/WindowManager(16460):     at sync.directtrac.NetError.showInfoMessageDialog(NetErrorPage.java:114)
    08-17 11:59:08.019: E/WindowManager(16460):     at sync.directtrac.NetError.onCreate(NetErrorPage.java:26)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.os.Looper.loop(Looper.java:130)
    08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.main(ActivityThread.java:3687)
    08-17 11:59:08.019: E/WindowManager(16460):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-17 11:59:08.019: E/WindowManager(16460):     at java.lang.reflect.Method.invoke(Method.java:507)
    08-17 11:59:08.019: E/WindowManager(16460):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    08-17 11:59:08.019: E/WindowManager(16460):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    08-17 11:59:08.019: E/WindowManager(16460):     at dalvik.system.NativeStart.main(Native Method)
    

    I have searched more... but i don't have any right idea to clear this.

    What i want is, when loading this page, layout should be added and dialog should be shown.

    Please help me to clear this error

    Note: I have tried this also

    @Override
        protected void onResume() {
        super.onResume();
            runOnUiThread(new Runnable() {
                public void run() {
                    showInfoMessageDialog("Please check your network connection","Network Alert");
                }
            });
    
        }
    
  • Haresh Chaudhary
    Haresh Chaudhary almost 12 years
    It's a totally Context Issue.Nothing Else.
  • Ponmalar
    Ponmalar almost 12 years
    Very Sorry Haresh, Check my edited code. My actvity name is "NetErrorPage"
  • Haresh Chaudhary
    Haresh Chaudhary almost 12 years
    @Ponmalar Please check the Edit
  • Haresh Chaudhary
    Haresh Chaudhary almost 12 years
    PLease Paste the whole Activity Code.
  • Ponmalar
    Ponmalar almost 12 years
    please check my edited code, tell me what i have to change in that
  • Ponmalar
    Ponmalar almost 12 years
    please check my edited code, tell me what i have to change in that
  • Ponmalar
    Ponmalar almost 12 years
    please check my edited code, tell me what i have to change in that
  • nandeesh
    nandeesh almost 12 years
    use this in onresume instead showInfoMessageDialog in oncreate
  • nandeesh
    nandeesh almost 12 years
    super call should always be at top of onresume
  • Chirag
    Chirag almost 12 years
    @Ponmalar , i checked your code and i think i got error . please override onPause method in this activity and dismiss your alert dialog in that. You should have create AlertDialog alertDialog variable at class level (means declared it before onCreate method and dimiss it in onPause Method . This Will solve your problem)
  • Ponmalar
    Ponmalar almost 12 years
    I got a solution.... your suggestion may give solution to others, so giving up vote
  • Ponmalar
    Ponmalar almost 12 years
    I got a solution.... your suggestion may give solution to others, so giving up vote
  • Ponmalar
    Ponmalar almost 12 years
    I got a solution.... your suggestion may give solution to others, so giving up vote
  • Chirag
    Chirag almost 12 years
    @Ponmalar what is the solution ? Can you describe here ?
  • android developer
    android developer about 11 years
    nice & short description why this happens
  • Lavekush Agrawal
    Lavekush Agrawal almost 9 years
    My understanding we can not called second dialog before closing first one ? please correct me if i am worng.
  • Ponmalar
    Ponmalar almost 9 years
    @Lavekush: We can open many dialogs at a time but if the dialog is in new activity and you are invoking intent to call that dialog will cause the error. if you are calling from the activity and the dialog is in the same activity will not cause the error i hope.
  • CoolMind
    CoolMind about 8 years
    Thanks. In my case instead of dialog.hide() must be written dialog.dismiss();
  • Wahib Ul Haq
    Wahib Ul Haq about 7 years
    If you see the code of cancel() function, it already calls dismiss() internally but additionally it will also call your DialogInterface.OnCancelListener (if registered). So giving preference to one over another won't theoretically make a difference.
  • FindOutIslamNow
    FindOutIslamNow almost 6 years
    Even pause() causes this with me
  • CoolMind
    CoolMind almost 6 years
    @FindOutIslamNow, what method do you mean? onPause()?
  • FindOutIslamNow
    FindOutIslamNow almost 6 years
    Yes, it causes window leak also for displayed windows / dialogs
  • CoolMind
    CoolMind almost 6 years
    @FindOutIslamNow, thanks. If you know a command that leads to onPause() you can write it as a new answer or here.
  • Reejesh
    Reejesh almost 3 years
    @Override protected void onPause() { super.onPause(); //APP WILL CRASH IF THIS IS NOT PRESENT loadingDialog.dismiss(); }