Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity has leaked window

Tags:

android

What i am trying to do is to get files from server. The same code is running f9 with sdcard but when i get files from thorugh thread then i am getting following error in my logcat.

My code is as follows:

public class Map extends Activity
{
   //
    private GraphicsView mGLView;

    //private GisGLRenderer m_GisRenderer;

    final static String RESULT_KEY="result";
    final static int REQ_CODE=1001;
     AlertDialog m=null;



    public class LoadFile  extends AsyncTask<String,String,String>
    {
        ProgressDialog Asycdialog = new ProgressDialog(Map.this);


        @Override
        protected void onPreExecute() {
            //set message of the dialog
            Asycdialog.setMessage("Loading File");
            Asycdialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
            //show dialog
            Asycdialog.show();
            super.onPreExecute();
        }

        protected void onProgressUpdate(String ... progress)
        {

        }

        protected String  doInBackground(String ... Params)
        {
            Map.this.mGLView.LoadProjectFile(AppFuncs.g_path);
            Map.this.mGLView.requestRender();
            return null;
        }
        protected void onPostExecute(String result)
        {
            Asycdialog.dismiss();

            super.onPostExecute(result);
           }
    }




    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mGLView =  (GraphicsView) findViewById(R.id.glview);


    }
  public void bt_Open(View v)
    {

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Load File");
        builder.setMessage("Choose an option to load file")
                .setCancelable(false)
                .setNegativeButton("Server",new DialogInterface.OnClickListener(){
                   public void onClick(DialogInterface dialog,int which)
                   {
                       Intent i= new Intent(Map.this,serv.class);
                       startActivityForResult(i,REQ_CODE);

                   }

                }

                )
                .setPositiveButton("SDcard",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int which)
                    {

                        Intent i= new Intent(Map.this,FileChooser.class);
                        startActivityForResult(i,REQ_CODE);

                    }

                }

                );

        final AlertDialog a=builder.create();
        a.show();

}
 protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
        //super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==REQ_CODE)
        {
            if(resultCode==RESULT_OK && data.getExtras().containsKey(RESULT_KEY))
            {

            //    Toast.makeText(this,data.getExtras().getString(RESULT_KEY),Toast.LENGTH_SHORT).show();
           //     this.mGLView.m_SelectedProjectPath =  AppFuncs.path;

                LoadFile f= new LoadFile();
                f.execute("");


             //   this.mGLView.LoadProjectFile(AppFuncs.path);
            }
        }
    }







ERROR/WindowManager(20040): Activity idtech.ESDN.Map has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416a97b0 that was originally added here
        android.view.WindowLeaked: Activity idtech.ESDN.Map has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416a97b0 that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:380)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
        at 

android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
        at android.view.Window$LocalWindowManager.addView(Window.java:547)
        at android.app.Dialog.show(Dialog.java:277)
        at idtech.ESDN.Map$LoadFile.onPreExecute(Map.java:59)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at idtech.ESDN.Map.onActivityResult(Map.java:221)
        at android.app.Activity.dispatchActivityResult(Activity.java:5194)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3180)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3227)
        at android.app.ActivityThread.access$1100(ActivityThread.java:137)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4838)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)
like image 887
Muneem Habib Avatar asked Nov 30 '22 03:11

Muneem Habib


2 Answers

Reason for happening this is :

You're trying to show a Dialog after you've exited an Activity.

Solution :

To call dismiss() on the Dialog you created before exiting the Activity, e.g. in onPause(). All windows & dialogs should be closed before leaving an Activity.

like image 200
Ritesh Gune Avatar answered Dec 05 '22 01:12

Ritesh Gune


Check that the Activity is not finishing before showing a Dialog otherwise it might leak :

    if(!this.isFinishing()){
        dialog.show();
    }

Also trying moving this declaration into the Activity, instead of the AsyncTask, and try dismissing the Dialog in the activity's onDestroy as well.

    ProgressDialog Asycdialog = new ProgressDialog(Map.this);
like image 39
Emil Davtyan Avatar answered Dec 05 '22 01:12

Emil Davtyan