Android 进度对话框未显示 [英] Android Progress Dialog not showing

查看:89
本文介绍了Android 进度对话框未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是主线程中的处理程序,用于显示和关闭进度对话框.

This is the handler, in the main Thread, which shows and dismisses a progress dialog.

public static final int SHOW = 0;
public static final int DISMISS = 1;
public Handler pdHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Log.i(TAG, "+ handleMessage(msg:" + msg + ")");
        switch(msg.what) {
        case SHOW:
            pd = ProgressDialog.show(LogViewer.this, "", getText(R.string.loading_msg), true);
            break;
        case DISMISS:
            if(pd != null) {
                pd.dismiss();
                pd = null;
            }
            break;
        }
    }
};

显示进度的消息是:

        pdHandler.sendMessage(pdHandler.obtainMessage(SHOW));

关闭它的消息是:

        pdHandler.sendMessage(pdHandler.obtainMessage(DISMISS));

当我在启动 AsyncTask 之前调用它时效果很好,AsyncTask onPostExecute() 会关闭它.

It works well when I call it before I start an AsyncTask, the AsyncTask onPostExecute() dismisses it.

但是当它在可运行对象 (runOnUiThread) 中运行时,该对话框不会显示.检查调试器上的 pd 变量,显示已创建,正在运行,可见,但实际不可见.

But when it runs within a runnable (runOnUiThread), the dialog does not show. Examining the pd variable on the debugger, it shows that is is created, it is running and it is visible, but in practice it is not visible.

有什么建议吗?

更新:
我做了我一开始就应该做的明显测试.我评论了 DISMISS 消息.并且进度对话框确实出现了.在 runnable 完成后,它出现为时已晚.
我现在不明白 DISMISS 消息确实取消了尚未可见的 ProgressDialog,这就是我没有看到它的原因.现在的问题是:我需要 ProgressDialog 在执行可运行代码之前显示.它不是那么简单.我的调用层次是这样的:

UPDATE:
I did the obvious test I should have done in the first place. I commented the DISMISS message. And the progress dialog did show up. It appeared too late, after the runnable was finished.
I undestand now that the DISMISS message did dismiss the not-yet-visible ProgressDialog, that's why I did not see it.
The question becomes now: I need the ProgressDialog to show BEFORE the runnable code is executed. And it is not so straight forward. My call hierarchy is like this:

onScrollEventChanged
    --> runOnUiThread ( 
        --> checkScrollLimits
            --> if need to scroll
                 show ProgressDialog "Loading"
                 get new data into table
                 dismiss ProgressDIalog
    )

我尝试过这样的事情:

onScrollEventChanged
    --> checkScrollLimits
        --> if need to scroll
             show ProgressDialog "Loading"
             --> runOnUiThread ( 
                 get new data into table
                 dismiss ProgressDIalog
             )

但是在 ProgressDialog 可以显示之前,关闭消息仍然到达那里.

But still the dismiss message got there before the ProgressDialog could show.

根据 Logcat,在 SHOW 消息到达和 DISMISS 消息到达之间有 5 秒的间隔.

According to Logcat there is a five second interval between the arrival of the SHOW message and the arrival of the DISMISS message.

更新二:
我虽然会使用 ProgressDialog 的 isShowing() 方法

UPDATE II:
I though I will use the isShowing() method of ProgressDIalog

pd = ProgressDialog.show(...)
while(!pd.isShowing());

但它根本没有帮助,即使对话框尚未显示,它也会返回 true.

But it does not help at all, it returns true even if the dialog is not showing yet.

推荐答案

我通过将代码转换为 AsyncTask 并使用 try/catch 块使其工作.

I made it work by turning the code into AsyncTask and using try/catch blocks.

这篇文章

我不确定 AsyncTask 是否有必要,但在我使用它之后,try/catch 块有所作为.

I am not sure if AsyncTask is necessary, but after I had it, the try/catch block made the difference.

这篇关于Android 进度对话框未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆