微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AlertDialog 不断崩溃

如何解决AlertDialog 不断崩溃

我正在尝试实现一个使用 AlertDialog 显示确认对话框的类(即“您确定要这样做吗?”)。 当我第一次调用它时,它按预期工作。但是,如果我稍后尝试再次调用它,它会因 W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed 错误而崩溃。

我可以在每次需要使用确认框时创建单独的 AlertDialogs,但这似乎违反直觉。 在做了一些搜索(现在是 2 天)之后,我还没有找到一个可行的解决方案。 有问题的课程:

public class ConfirmDialog  {

    private boolean resultValue;
    private boolean result;
    private String title;
    private String message;
    private Context context;

    public ConfirmDialog(Context nContext,String nTitle,String nMessage) {
        context = nContext;
        title = nTitle;
        message = nMessage;
    }

    public boolean getResult()
    {
        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                throw new RuntimeException();
            }
        });

        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setMessage(message);
        alert.setPositiveButton("Yes",new
                DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog,int id)
                    {
                        resultValue = true;
                        handler.sendMessage(handler.obtainMessage());
                    }
                });
        alert.setNegativeButton("No",new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog,int id)
            {
                resultValue = false;
                handler.sendMessage(handler.obtainMessage());
            }
        });
        alert.show();

        try{ Looper.loop();}

        catch(RuntimeException e){}

        return resultValue;
    }
}

调用类:

ConfirmDialog dialog = new ConfirmDialog(MainMenu.this,"Upload Batch","Are you sure you want to upload this batch?");
                if(dialog.getResult() == false){
                    return false;
                }
                else {
                    //do something                
                }

我继承了这个项目,没有创建这个类。我也认为我什至不需要使用它,但如果我可以让它工作,它会让事情变得更容易。

有什么想法吗?

谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。