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

Ponmalar picture Ponmalar · Aug 17, 2012 · Viewed 131.3k times · Source

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

    }

Answer

Ponmalar picture Ponmalar · Aug 17, 2012

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 :-).