如何调试FragmentContainerActivity已泄露窗口

时间:2017-01-11 11:47:11

标签: android

生成此日志错误的代码。

final Dialog dialog = new Dialog(getActivity());
dialog.show();

错误日志在下面。

E/WindowManager: android.view.WindowLeaked: Activity com.eonelectric.eon.FragmentContainerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{201c1a84 V.E..... R.....ID 0,0-897,540} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:375)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.eonelectric.eon.fragments.BeatRecordFragment.showOverLay(BeatRecordFragment.java:2360)
at com.eonelectric.eon.fragments.BeatRecordFragment.onCreateView(BeatRecordFragment.java:259)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
at com.eonelectric.eon.GPlusLogoutHelperGCActivity.onStart(GPlusLogoutHelperGCActivity.java:48)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1259)
at android.app.Activity.performStart(Activity.java:6026)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

如何修复此错误?它在哪里被泄露?是什么造成的?

1 个答案:

答案 0 :(得分:1)

当活动结束并进入后台时,您正在尝试显示对话框。 内部活动检查活动是否正在完成。

if(isFinishing()) return;
final Dialog dialog = new Dialog(getActivity());
dialog.show();
在片段中你可以写:

if(getActivity() != null && !getActivity().isFinishing()) {
    final Dialog dialog = new Dialog(getActivity());
    dialog.show();   
}