0

My app was working fine but all of sudden it crashes and this error is getting me more crazy. it was working well both on server and on local files. but now it crashes whenver i try to read file from server.

My logcat is as follows:

ERROR/WindowManager(10324): Activity idtech.ESDN.Map has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41838b78 that was originally added here
        android.view.WindowLeaked: Activity idtech.ESDN.Map has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41838b78 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:64)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at idtech.ESDN.Map.onActivityResult(Map.java:241)
        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)

Its showing that error is coming on async task which is as follows:

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


    @Override
    protected void onPreExecute() {
        //set message of the dialog

        super.onPreExecute();
        Asycdialog.setMessage("Loading File");
        Asycdialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        //show dialog
       if (!Map.this.isFinishing())
       {
           Asycdialog.show();
       }
    }

    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);
       }
}
Arun C
  • 9,035
  • 2
  • 28
  • 42
Muneem Habib
  • 1,046
  • 4
  • 18
  • 49
  • look onto this http://stackoverflow.com/questions/11731504/android-leaked-window and this http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added – surhidamatya Aug 26 '13 at 06:20
  • normally this kind of leak error comes if we forget to unregister something. Is there something in your case whihc you need to unregister? – Sushil Aug 26 '13 at 06:21
  • It seems like your activity is destroyed but don't dismiss a shown dialog. Dialog's lifecycle should be aligned w/ host Activity's lifecycle. So, make sure to dismiss all dialog properly in Activity's onPause callback. – Chansuk Aug 26 '13 at 06:26
  • you have to dismis progres dialob box in postexecution after super.onPostExecute – PankajAndroid Aug 26 '13 at 06:26
  • This happened to me once with emulator. If the same code worked previously and if it causing trouble then you should try restarting your emulator.. This helped in my case – Andro Selva Aug 26 '13 at 06:32
  • Probably what is happening is: (1) You show the dialog in `onPreExecute()`, (2) The background task begins, (3) Meanwhile the Activity is finished, (4) The window manager removes the Activity's window from the screen and detects that the dialog has not yet been dismissed and throws an exception as a result. – Alex Lockwood Aug 26 '13 at 06:35

2 Answers2

1

A window leak occurs in this case if your preExecute/postExecute runs after Activity is finished. Try using a WeakReference to the activity and move dialog creation code into onCreateDialog method of the activity. However, onCreateDialog is deprecated and you should move to a DialogFragment.

private int DIALOG_ID_PROGRESS = 1;
@Override
protected Dialog onCreateDialog(int id) {
    if (id == DIALOG_ID_PROGRESS) {
        ProgressDialog pd = new ProgressDialog(this);
        pd.setMessage("Loading...");
        return pd;
    }
    return super.onCreateDialog(id);
}

private class GetDataTask extends AsyncTask<Void, Void, Void> {
    private WeakReference<Activity> mActivityReference;
    GetDataTask(Activity activity) {
        mActivityReference = new WeakReference<Activity>(activity);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Activity activity = mActivityReference.get();
        if(activity != null) {
            activity.showDialog(DIALOG_ID_PROGRESS);
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Activity activity = mActivityReference.get();
        if(activity != null) {
            activity.dismissDialog(DIALOG_ID_PROGRESS);
        }
    }
}
Rajiv
  • 306
  • 2
  • 5
-1

I think this will help u.try this and tell.You're trying to show a Dialog after you've exited an Activity.

  protected void onPostExecute(String result)
     {
       if(Asycdialog!=null)
          {
            Asycdialog.dismiss();                    
           }
        super.onPostExecute(result);
          }
     }
Ruban
  • 1,514
  • 2
  • 14
  • 21