4

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)
Muneem Habib
  • 1,046
  • 4
  • 18
  • 49
  • possible duplicate of [Activity has leaked window that was originally added](http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Nabin Jun 20 '14 at 07:11

2 Answers2

16

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.

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
4

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);
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • @MuneemHabib It makes no difference, just access the activity object by changing the `this` to `Map.this` from your posted code and check if the activity is finishing or finished already before displaying your dialog. – Emil Davtyan Aug 20 '13 at 09:09
  • i have done checking activity is finished or not but i am getting same result. – Muneem Habib Aug 20 '13 at 09:38
  • @MuneemHabib Check addition in answer about moving `Asycdialog`. – Emil Davtyan Aug 20 '13 at 09:57
  • what u mean by moving dialog? my code is working fine with sdcard but it is crashing in case of geeting fi;les from server its just because of server – Muneem Habib Aug 20 '13 at 10:25
  • @MuneemHabib If the request is taking too long, the async task's onPostExecute is not running before the activity is destroyed. You should both cancel the asynctask and dismiss the dialog in the activities onPause, thats why you should move the dialog out of the AsyncTask, or override its cancel(). – Emil Davtyan Aug 20 '13 at 10:32