简体   繁体   English

我认为应该显示Android AlertDialog时未显示

[英]Android alertdialog not showing when I think it should

I'm developing an android app which I want to check if there's internet connection, if it's not the case display a warning message, and when there's internet connection again load a certain url. 我正在开发一个Android应用程序,我想检查是否存在互联网连接,如果不是这种情况,则显示警告消息,并在有互联网连接时再次加载特定的网址。

It can be said that both displaying the message and checking there's internet connection work... but no separately. 可以说,既显示消息又检查互联网连接是否正常……但是没有单独进行。

I mean I have the following code: 我的意思是我有以下代码:

      if (!checkConnectivity())
                {

                    browser.stopLoading();


                    LayoutInflater layoutinflater = LayoutInflater.from(app);
                    final View textEntryView;


                        textEntryView = layoutinflater.inflate(R.layout.exitdialog, null);
                        new AlertDialog.Builder(app)
                            .setView(textEntryView)
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setTitle("Exit")                               
                            .setNegativeButton("CANCEL", null)
                            .show();



                        while (!checkConnectivity())
                        {

                        }

                            browser.loadUrl(url);


                }

If I don't use from while (!checkConnectivity()) to browser.loadUrl(url); 如果我不使用while(!checkConnectivity())到browser.loadUrl(url); then AlertDialog shows properly. 然后AlertDialog会正确显示。

If I don't use from LayoutInflater layoutinflater = LayoutInflater.from(app); 如果我不使用LayoutInflater,则layoutinflater = LayoutInflater.from(app); to .show(); 显示(); then the app loads the web page correctly on browser. 然后该应用会在浏览器上正确加载网页。

But if I use the whole code it looks like it enters the while (!checkConnectivity()) loop before it does the show as what happens when internet connection is restabilished is that the alert dialog is shown briefly and the the web page is loaded. 但是,如果我使用整个代码,则它看起来像进入了while(!checkConnectivity())循环,然后再显示,因为重新建立Internet连接时会短暂显示警报对话框并加载网页。

I find this pretty weird, as I'm not using, as far as I know, threads that could cause that one could be executed before other one, and this is not the case this thing doesn't fulfill one of the most basic things in programming, I mean if an instruction is before another one, the one that's before should be plenty executed logically (except for threads). 据我所知,我发现这很奇怪,因为我没有使用可能导致一个线程可以在另一个线程之前执行的线程,而这不是这种事情不能满足最基本的事情之一的情况在编程中,我的意思是,如果一条指令在另一条指令之前,则该指令之前的指令应在逻辑上进行大量执行(线程除外)。

Hope someone has an idea about this and can help me. 希望有人对此有想法,可以为我提供帮助。

Your while loop blocks the UI thread and the dialog has no chance to get displayed. 您的while循环会阻塞UI线程,并且对话框没有机会显示。

Busy-looping to poll for a condition is also a big red flag. 忙循环查询条件也是一个大危险信号。

Consider waiting for a connectivity event , and use a cancellable progress dialog instead of an an alert dialog to disable other UI until the event is received. 考虑等待连接事件 ,并使用可取消进度对话框而不是警报对话框来禁用其他UI,直到接收到该事件为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM