简体   繁体   English

应用程式因视窗泄漏而当机

[英]App crashes due to leaked window

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: 我的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)

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

A window leak occurs in this case if your preExecute/postExecute runs after Activity is finished. 如果在活动完成后运行preExecute / postExecute,则在这种情况下会发生窗口泄漏。 Try using a WeakReference to the activity and move dialog creation code into onCreateDialog method of the activity. 尝试对活动使用WeakReference并将对话框创建代码移动到活动的onCreateDialog方法中。 However, onCreateDialog is deprecated and you should move to a DialogFragment. 但是,不建议使用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);
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM