应用程序因泄漏的窗口而崩溃

时间:2013-08-26 06:16:21

标签: android

我的应用程序工作正常,但突然间它崩溃了,这个错误让我更加疯狂。 它在服务器和本地文件上运行良好。但是现在当我尝试从服务器读取文件时它崩溃了。

我的logcat如下:

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)

它显示错误来自异步任务,如下所示:

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

2 个答案:

答案 0 :(得分:1)

如果在Activity完成后运行preExecute / postExecute,则会发生窗口泄漏。尝试对活动使用WeakReference并将对话框创建代码移动到活动的onCreateDialog方法中。但是,不推荐使用onCreateDialog,您应该转到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);
        }
    }
}

答案 1 :(得分:-1)

我认为这有助于你这么做并告诉你。你在退出活动后试图显示一个Dialog。

  protected void onPostExecute(String result)
     {
       if(Asycdialog!=null)
          {
            Asycdialog.dismiss();                    
           }
        super.onPostExecute(result);
          }
     }