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

AndroidAndroid LayoutMemory LeaksAndroid Alertdialog

Android Problem Overview


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");
		    }
		});
		
	}

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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

Solution 4 - Android

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 - Android

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

Solution 6 - Android

 @Override
    protected void onPostExecute(final Boolean success) {
        mProgressDialog.dismiss();
        mProgressDialog = null;

setting the value null works for me

Solution 7 - Android

I got this error but it is resolved interesting. As first, i got this error at api level 17. When i call a thread (AsyncTask or others) without progress dialog then i call an other thread method again using progress dialog, i got that crash and the reason is about usage of progress dialog.

In my case, there are two results that;

  • I took show(); method of progress dialog before first thread starts then i took dismiss(); method of progress dialog before last thread ends.

So :

ProgresDialog progressDialog = new ...

//configure progressDialog 

progressDialog.show();

start firstThread {
...
}

...

start lastThread {
...
} 

//be sure to finish threads

progressDialog.dismiss();
  • This is so strange but that error has an ability about destroy itself at least for me :)

Solution 8 - Android

The way I got around this issue is by not calling intent within a dialog. **** use syntax applicable to activity or fragment accordingly

@Override
public void onClick(DialogInterface dialog, int which) {
    checkvariable= true;
    getActivity().finish();
}
                                
@Override
public void onStop() {
    super.onStop();
    if (checkvariable) {
        startActivity(intent); 
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPonmalarView Question on Stackoverflow
Solution 1 - AndroidPonmalarView Answer on Stackoverflow
Solution 2 - AndroidChiragView Answer on Stackoverflow
Solution 3 - AndroidCoolMindView Answer on Stackoverflow
Solution 4 - AndroidHaresh ChaudharyView Answer on Stackoverflow
Solution 5 - AndroidnandeeshView Answer on Stackoverflow
Solution 6 - AndroidCarlos RojasView Answer on Stackoverflow
Solution 7 - AndroidoguzhanView Answer on Stackoverflow
Solution 8 - AndroidDavid OnyangoView Answer on Stackoverflow