gpt4 book ai didi

android - 通过(弹出)菜单退出应用程序时窗口泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:02 24 4
gpt4 key购买 nike

我希望你能帮助我,当我通过工具栏中的(弹出)菜单选项退出我的应用程序时,我有一个泄漏的窗口。如果我通过正常方法退出应用程序,一切都很好。

有人可以帮忙吗?我似乎找不到错误,我正在尝试找到合适的解决方案。我声明了 2 个静态字符串变量,但我猜这不是问题所在。

提前致谢。

这里是菜单的相关代码:

public class LoginActivity extends AppCompatActivity {
//
private Object mState = null;
private Object HIDE_MENU = null;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
//
@Override
public boolean onCreateOptionsMenu(Menu menu) {

// inflate menu from xml
MenuInflater mInflater = this.getMenuInflater();
mInflater.inflate(R.menu.menu_main, menu);

if (mState == HIDE_MENU) {
menu.getItem(0).setVisible(false);
}
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.settings:
// Unassigned
return true;

case R.id.exit:
this.finish();
return true;

default:
return super.onOptionsItemSelected(item);
}
}
//
}

日志:

E/WindowManager: android.view.WindowLeaked: Activity myapp.example.com.myapp.LoginActivity has leaked window android.widget.PopupWindow$PopupDecorView{1f4c330 V.E...... ........ 0,0-686,168} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:368)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1258)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1110)
at android.support.v7.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:78)
at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:30)
at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:92)
at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:171)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:680)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:163)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:781)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

编辑,我的菜单xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="myapp.example.com.myapp.MainActivity">
<item
android:id="@+id/settings"
android:title="Instellingen"
app:showAsAction="never" />
<item
android:id="@+id/exit"
android:title="Exit"
app:showAsAction="never" />
</menu>

EDIT2,我通过在关闭应用程序时添加缓慢的延迟来解决自己。显然,菜单窗口在应用程序退出之前没有机会正确关闭,是什么触发了泄漏。可能有更方便的方法,但这行得通。

//
case R.id.exit:

Handler handlerClose = new Handler();
handlerClose.postDelayed(new Runnable() {
public void run() {
finish();
}
},300);
return true;
//

最佳答案

尝试在调用 finish() 之前调用 closeOptionsMenu():

closeOptionsMenu();
finish();

这对我有用,但可能会有某种 race condition给你带来麻烦。如果是这样,这应该可以代替:

private boolean exit = false;


@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);

if( exit )
finish();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.settings:
// Unassigned
return true;

case R.id.exit:
exit = true;
closeOptionsMenu();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

关于android - 通过(弹出)菜单退出应用程序时窗口泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36676412/

24 4 0