无法关闭我的 Android 应用程序中的对话框

发布于 2024-12-25 02:52:47 字数 1242 浏览 1 评论 0原文

我这里有一些代码(我的活动类和一些扩展 WebViewClient 的类) 因此,在我的活动中,我做了这样的事情:

protected Dialog onCreateDialog(int id) {
    switch(id) {
        case 1:
            //logging vk dialog
            Log.d("OLOLOLO", "webview"); 
            dialog = new Dialog(this);
            dialog.setContentView(R.layout.webviewl);
            dialog.setTitle("loggin in");

            webview = (WebView) dialog.findViewById(R.id.vkWebView);
            webview.setWebViewClient(wvClforVK);  
            webview.loadUrl(url);
            // do the work to define the pause Dialog 
            break;
        case 2:
            // already logged vk dialog
            break;
        default:
            dialog = null;
    }
    return dialog;
}

然后在某个 buttonclick 侦听器上调用 showDialog(1)

在我的 webview 类中的 onPageFinished() 方法中,我需要关闭对话框,但我认为这样做是不正确的:

MyActivity activity = new MyActivity();  //my main activity object
activity.dismissDialog(1);

它不起作用:

01-03 20:41:10.758: E/AndroidRuntime(1172): java.lang.IllegalArgumentException: 没有通过 Activity#showDialog 显示 id 1 的对话框

我如何才能正确显示我的活动对象关闭对话框?

I have some code here (my activity class and some class, that extends WebViewClient)
so, in my activity I do something like this:

protected Dialog onCreateDialog(int id) {
    switch(id) {
        case 1:
            //logging vk dialog
            Log.d("OLOLOLO", "webview"); 
            dialog = new Dialog(this);
            dialog.setContentView(R.layout.webviewl);
            dialog.setTitle("loggin in");

            webview = (WebView) dialog.findViewById(R.id.vkWebView);
            webview.setWebViewClient(wvClforVK);  
            webview.loadUrl(url);
            // do the work to define the pause Dialog 
            break;
        case 2:
            // already logged vk dialog
            break;
        default:
            dialog = null;
    }
    return dialog;
}

and then call showDialog(1) on some buttonclick listener.

In my webview class in onPageFinished() method I need to dismiss my dialog but I think it will be incorrect to do this:

MyActivity activity = new MyActivity();  //my main activity object
activity.dismissDialog(1);

It doesn't work:

01-03 20:41:10.758: E/AndroidRuntime(1172): java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog

How can I get my activity object to correctly dismiss the dialog?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2025-01-01 02:52:47

问题是您实例化了一个没有任何对话框的新活动。您必须在创建对话框的同一活动实例上调用missDialog 方法。如果您在另一个类中调用它,则必须以某种方式将您的活动传递给该类(例如,您可以将其作为参数传递)。无论如何,不​​建议以这种方式实例化活动,如果您在项目的清单文件中定义了它们,它们会自动实例化。

The problem is that you instantiate a new activity which doesn't have any dialog. You have to call the dismissDialog method on the same activity instance in which you created the dialog. If you call it in another class, you have to pass your activity somehow to that class (for example you can pass it as a parameter). Anyway, it is not recommended to instantiate activities in this way, they are instantiated automatically if you defined them in the manifest file of your project.

↙厌世 2025-01-01 02:52:47

正如异常所示,您试图关闭在使用 showDialog 之前未显示的对话框。您需要检查对话框的生命周期。您可以使用 Dialog.isShowing() 方法来确认对话框在关闭之前是否显示。

As the exception says, you are trying to dismiss a dialog that was not shown before using showDialog. You need to check the life cycle of the dialog. You can use Dialog.isShowing() method to confirm the dialog is shown before dismissing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文