这个错误是什么,为什么会发生?

05-17 18:24:57.069: ERROR/WindowManager(18850): Activity com.mypkg.myP has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here
05-17 18:24:57.069: ERROR/WindowManager(18850): android.view.WindowLeaked: Activity ccom.mypkg.myP has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.ViewRoot.<init>(ViewRoot.java:231)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.Dialog.show(Dialog.java:239)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.mypkg.myP$PreparePairingLinkageData.onPreExecute(viewP.java:183)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.os.AsyncTask.execute(AsyncTask.java:391)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.mypkg.myP.onCreate(viewP.java:94)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.os.Looper.loop(Looper.java:123)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at java.lang.reflect.Method.invoke(Method.java:521)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at dalvik.system.NativeStart.main(Native Method)

当前回答

唯一的答案是

  @Override
    public void onBackPressed() {

        exit_popup();
       // super.onBackPressed(); remove this line

    }

其他回答

您试图在退出活动后显示一个对话框。

(编辑)

这个问题是谷歌上android开发者搜索最多的问题之一, 因此,从评论中添加一些重要的观点,这可能对未来的调查人员更有帮助,而不需要深入评论对话。

答案1:

您试图在退出活动后显示一个对话框。

回答2

在某些情况下,这个错误可能会有点误导(尽管 答案仍然是完全准确的)-即在我的情况下,一个 在AsyncTask中抛出了unhandled异常,这导致 活动关闭,然后一个打开的progressdialog导致了这一点 异常. .所以'real'异常在日志中稍早一些

回答3

在退出前创建的Dialog实例上调用dismiss() 活动,例如在onPause()或onDestroy()中

当我在AsyncTask中使用ProgressDialog时,这种情况发生在我身上。实际上,我在onPostExecute中使用hide()方法。基于@Alex Volovoy的答案,我需要使用遣散()与ProgressDialog删除它在onPostExecute和它完成。

progressDialog.hide(); // Don't use it, it gives error

progressDialog.dismiss(); // Use it

解决方案是在退出Activity之前在view .java:183中创建的对话框上调用dismiss(),例如在onPause()中。所有的窗口和对话框应该在离开一个活动之前关闭。

试试下面的代码:

public class Sample extends Activity(){
@Override
 public void onCreate(Bundle instance){

}
 @Override
    public void onStop() {
        super.onStop();
      progressdialog.dismiss(); // try this
    }

}

最好的解决方案是把它放在显示progressbar或progressDialog之前

if (getApplicationContext().getWindow().getDecorView().isShown()) {

  //Show Your Progress Dialog

}