ProgressDialog 未显示在活动中

发布于 2024-10-15 20:58:38 字数 521 浏览 3 评论 0原文

我正在尝试在我的应用程序中包含 ProgressDialog。但它没有出现。

这是我使用 ProgressDialog 的代码片段:

public class abcActivity extends Activity {
    public boolean onOptionsItemSelected(MenuItem item) {
        case XYZ:
            ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true);
            callSomeFunction();
            dialog.dismiss();
            showToast(getString(R.string.SomeString));
            break;
    }
}

有谁知道为什么对话框没有显示?有什么线索吗?

I am trying to include a ProgressDialog in my application. But it is not showing up.

Here's the code snippet where i use the ProgressDialog:

public class abcActivity extends Activity {
    public boolean onOptionsItemSelected(MenuItem item) {
        case XYZ:
            ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true);
            callSomeFunction();
            dialog.dismiss();
            showToast(getString(R.string.SomeString));
            break;
    }
}

Does anyone know why the dialog is not showing up? Any clues?

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

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

发布评论

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

评论(4

夏の忆 2024-10-22 20:58:38

我认为您的代码在某种意义上是错误的,因为您在 UI 线程中完成了所有操作。您必须将 callsomefunction() 放入后台线程。

public void runSomething()
{
    showDialog(BACKGROUND_ID);
    Thread t = new Thread(new Runnable() 
    {                   
        public void run() 
        {
            //do something
            handler.post(finishThread);
        }
    });

    t.start();
    // The progress wheel will only show up once all code coming here has been executed
}

还有

protected Dialog onCreateDialog(int id)
{
    if(progressDialog == null) progressDialog = new ProgressDialog(this);
    return progressDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
    if(id == BACKGROUND_ID) 
    {
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("running for long ...");
    }
}

Runnable finishThread = new Runnable()
{       
    public void run() 
    {
//long running
        if(progressDialog != null) progressDialog.dismiss();
    }
};

I think your code is wrong in a sense that you do all in the UI thread. You have to put callsomefunction() into a background thread.

public void runSomething()
{
    showDialog(BACKGROUND_ID);
    Thread t = new Thread(new Runnable() 
    {                   
        public void run() 
        {
            //do something
            handler.post(finishThread);
        }
    });

    t.start();
    // The progress wheel will only show up once all code coming here has been executed
}

And as well

protected Dialog onCreateDialog(int id)
{
    if(progressDialog == null) progressDialog = new ProgressDialog(this);
    return progressDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
    if(id == BACKGROUND_ID) 
    {
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("running for long ...");
    }
}

Runnable finishThread = new Runnable()
{       
    public void run() 
    {
//long running
        if(progressDialog != null) progressDialog.dismiss();
    }
};
烏雲後面有陽光 2024-10-22 20:58:38

我认为问题出在您的开关状态上,请验证它。

这是在 android 中显示对话框的另一种方法,试试这个。

public static final int DIALOG2_KEY = 1;
public static ProgressDialog dialog1;

showDialog(DIALOG2_KEY); // use it where you want to display dialog 

dialog1.cancel();  // use it to cancel the dialog


@Override
    public Dialog onCreateDialog(int id) {
        if (id == 1) {
            dialog1 = new ProgressDialog(this);
            dialog1.setMessage("Please wait...");
            dialog1.setIndeterminate(true);
            dialog1.setCancelable(true);
        }
        return dialog1;
    }

i think issue is in your switch condition pls verify it.

here is another method to display dialog in android try this.

public static final int DIALOG2_KEY = 1;
public static ProgressDialog dialog1;

showDialog(DIALOG2_KEY); // use it where you want to display dialog 

dialog1.cancel();  // use it to cancel the dialog


@Override
    public Dialog onCreateDialog(int id) {
        if (id == 1) {
            dialog1 = new ProgressDialog(this);
            dialog1.setMessage("Please wait...");
            dialog1.setIndeterminate(true);
            dialog1.setCancelable(true);
        }
        return dialog1;
    }
万劫不复 2024-10-22 20:58:38

确保您通过调试或添加日志来调用此代码。该代码对我来说似乎是正确的。

另外,如果您想在后台执行某些操作并在执行时显示进度对话框,请使用带有 ProgressDialog 边界的 AsyncTask,例如 此处

Ensure you have this code called by debugging or adding a log. The code seems right to me.

Also, if you want to perform some operations in background and show a progress dialog while the performing, please use AsyncTask with ProgressDialog bounded, like here.

故事未完 2024-10-22 20:58:38

ProgressDialog 在 Android O 中已弃用,现在使用 ProgressBar。

ProgressDialog is deprecated in Android O , Use ProgressBar now.

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