10

我有一个PopupMenu锚定到操作栏中的按钮,并且我正在获取有关泄漏窗口的日志输出。

如果我的活动在PopupMenu打开时停止,则会发生这种情况。

我在这里发布了一个相关的问题,但我已经解决了这个问题。

我见过一些类似的问题,例如this,但与PopupMenu.

我在想如果我能处理提到的问题,PopupWindow$PopupViewContainer那么我可以将它从WindowManageronPause 期间或类似的东西中删除,但我不知道如何处理它。暴露的接口PopupMenu非常有限。

  • 有没有人遇到过这个问题?
  • 有没有人修复它或解决它?
  • 任何人都知道如何获得PopupMenu's Viewor Window

这就是我创建菜单的方式:

// in Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    final MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.main_menu, menu);
    MenuItem login = menu.findItem(R.id.menu_login);
    final Button button = (Button) login.getActionView().findViewById(R.id.login);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View item) {
            if (profileMenu == null) {
                // I've tried passing getApplicationContext() here too, with no change
                profileMenu = new PopupMenu(ListActivity.this, button);
                profileMenu.getMenuInflater().inflate(R.menu.profile_menu, profileMenu.getMenu());
                profileMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        // there was code here, but I removed it and problem persists
                        return false;
                    }
                });
            }
            profileMenu.show();

        }
    });

这是完整的堆栈跟踪:

10-24 11:10:13.878: E/WindowManager(2048): Activity **.app.ListActivity has leaked window android.widget.PopupWindow$PopupViewContainer@4157a7e8 that was originally added here
10-24 11:10:13.878: E/WindowManager(2048): android.view.WindowLeaked: Activity **.app.ListActivity has leaked window android.widget.PopupWindow$PopupViewContainer@4157a7e8 that was originally added here
10-24 11:10:13.878: E/WindowManager(2048):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
10-24 11:10:13.878: E/WindowManager(2048):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
10-24 11:10:13.878: E/WindowManager(2048):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
10-24 11:10:13.878: E/WindowManager(2048):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
10-24 11:10:13.878: E/WindowManager(2048):  at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
10-24 11:10:13.878: E/WindowManager(2048):  at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:897)
10-24 11:10:13.878: E/WindowManager(2048):  at android.widget.ListPopupWindow.show(ListPopupWindow.java:595)
10-24 11:10:13.878: E/WindowManager(2048):  at com.android.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:127)
10-24 11:10:13.878: E/WindowManager(2048):  at com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:100)
10-24 11:10:13.878: E/WindowManager(2048):  at android.widget.PopupMenu.show(PopupMenu.java:108)
10-24 11:10:13.878: E/WindowManager(2048):  at **.app.ListActivity$3.onClick(ListActivity.java:376)
10-24 11:10:13.878: E/WindowManager(2048):  at android.view.View.performClick(View.java:4084)
10-24 11:10:13.878: E/WindowManager(2048):  at android.view.View$PerformClick.run(View.java:16966)
10-24 11:10:13.878: E/WindowManager(2048):  at android.os.Handler.handleCallback(Handler.java:615)
10-24 11:10:13.878: E/WindowManager(2048):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 11:10:13.878: E/WindowManager(2048):  at android.os.Looper.loop(Looper.java:137)
10-24 11:10:13.878: E/WindowManager(2048):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 11:10:13.878: E/WindowManager(2048):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 11:10:13.878: E/WindowManager(2048):  at java.lang.reflect.Method.invoke(Method.java:511)
10-24 11:10:13.878: E/WindowManager(2048):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 11:10:13.878: E/WindowManager(2048):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 11:10:13.878: E/WindowManager(2048):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

11

只需调用 profileMenu.dismiss() onStop() 就足够了。

于 2012-10-24T18:39:01.130 回答
2

对我来说 .dismiss() 它在 onDestroy() 没有帮助。

事实证明,消除 PopupMenu 泄漏窗口问题的唯一方法是删除进入/退出转换,我将它们替换为 popupAnimationStyle。

这解决了这个泄漏的窗口问题。

<item name="android:popupEnterTransition">@null</item>
<item name="android:popupExitTransition">@null</item>
<item name="android:popupAnimationStyle">@style/PopupMenuAnimation</item>
    
<style name="PopupMenuAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_bottom</item>
    <item name="android:windowExitAnimation">@anim/slide_down_bottom</item>
</style>
于 2021-02-12T07:26:08.070 回答