gpt4 book ai didi

java - android.view.WindowLeaked 提示对话框错误原因

转载 作者:行者123 更新时间:2023-11-29 04:27:32 26 4
gpt4 key购买 nike

我是 android 的新手,我对警报对话框有疑问。有一种方法是这样的:

private void deletSelected(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("are you sure?");
dialog.setPositiveButton("Yes" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
int deletedRow = getContentResolver().delete(
ContentUris.withAppendedId(CONTENT_URI , selectedRow) , null , null);

if (deletedRow == -1){
Toast.makeText(EditorActivity.this , "not deleted!" , Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(EditorActivity.this , "deleted successfully" , Toast.LENGTH_SHORT).show();
}
if (dialog != null){
dialog.dismiss();
}
}
});
dialog.setNegativeButton("no" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null){
dialog.dismiss();
}
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}

第二种方法是这样的:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Save" menu option
case R.id.action_save:
save();
finish();
return true;
// Respond to a click on the "Delete" menu option
case R.id.action_delete:
deletSelected();
finish(); // if i remove this line app will work correctly
return true;
}
return super.onOptionsItemSelected(item);
}

我的问题是,如果我在 onOptionsItemSelected 方法中删除 finish(),我的应用程序将正确运行,但它仍处于我不想要的当前 Activity 中。我希望用户在确认对话框中按是后删除数据库中的选定行并返回主 Activity ,但如果没有此行,选定行将删除但应用程序不会返回到 mainActivity 。

如果我添加 finish(),确认对话框会在单击删除后立即消失,应用程序将返回主 Activity 而不删除任何内容。逻辑上显示以下错误:

08-04 21:57:02.630 28064-28064/com.example.android.pets E/WindowManager: android.view.WindowLeaked: Activity com.example.android.pets.EditorActivity has leaked window android.widget.PopupWindow$PopupDecorView{652e345 V.E...... ......ID 0,0-588,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:394)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:330)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1279)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1118)
at android.support.v7.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:105)
at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:33)
at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:129)
at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:206)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:721)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:682)
08-04 21:57:02.631 28064-28064/com.example.android.pets E/WindowManager: android.view.WindowLeaked: Activity com.example.android.pets.EditorAc

有什么想法吗?

编辑:删除 finish() 并将其放入 onClick 方法后。现在一切都正确了。但我不明白这两种方式有什么区别?

最佳答案

尝试将您想要完成的 Activity 传递给 deletSelected() 方法,并从 onClick() 调用 activity.finish() >,而不是从当前位置调用 finish()

关于java - android.view.WindowLeaked 提示对话框错误原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com