Android Activity已泄露窗口

时间:2013-08-20 08:42:17

标签: android

我想要做的是从服务器获取文件。相同的代码使用sdcard运行f9,但是当我从thorugh线程获取文件时,我的logcat中出现了以下错误。

我的代码如下:

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)

2 个答案:

答案 0 :(得分:14)

发生这种情况的原因是:

您在退出活动后尝试显示Dialog

解决方案:

要在退出dismiss()之前致电您Dialog上的Activity,例如在onPause()中。所有窗户和在离开Activity之前应该关闭对话框。

答案 1 :(得分:4)

在显示Activity之前检查Dialog是否未完成,否则可能会泄漏:

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

同时尝试将此声明移至Activity而不是AsyncTask,并尝试同时取消活动Dialog中的onDestroy

    ProgressDialog Asycdialog = new ProgressDialog(Map.this);