ProgressDialog 不显示。再次

发布于 2025-01-07 03:17:37 字数 914 浏览 0 评论 0原文

我很困惑,因为这在其他活动中对我来说非常有效,在这里我基本上只是复制粘贴代码,但 ProgressDialog 没有显示。这是代码:

public class MyListActivity extends ListActivity {  
  public void onCreate(Bundle savedInstanceState) 
         {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.mylayout);          
             final ProgressDialog progress = new ProgressDialog(this);        
             progress.setProgressStyle(STYLE_SPINNER);
             progress.setIndeterminate(true);
             progress.setMessage("Working...");
             progress.show();
             Thread thread = new Thread() 
                {          
                  public void run() 
                  {

                      //long operation populating the listactivity
                      progress.dismiss();
                  }
                };
                thread.run();               
         }
}

I'm confused because this has been working for me quite fine in other activities, and here I just basically copy-pasted the code, but ProgressDialog doesn't show up. Here's the code:

public class MyListActivity extends ListActivity {  
  public void onCreate(Bundle savedInstanceState) 
         {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.mylayout);          
             final ProgressDialog progress = new ProgressDialog(this);        
             progress.setProgressStyle(STYLE_SPINNER);
             progress.setIndeterminate(true);
             progress.setMessage("Working...");
             progress.show();
             Thread thread = new Thread() 
                {          
                  public void run() 
                  {

                      //long operation populating the listactivity
                      progress.dismiss();
                  }
                };
                thread.run();               
         }
}

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

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

发布评论

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

评论(3

长途伴 2025-01-14 03:17:37

不确定这是否是问题的根本原因,但请尝试执行 thread.start() 而不是 thread.run()。执行 start() 实际上会启动一个新线程,并且可能会给进度对话框一个显示的机会。

Not sure if this is the root cause of your problem, but try executing thread.start() instead of thread.run(). Executing start() will actually start a new thread and maybe give the progress dialog a chance to show.

沉睡月亮 2025-01-14 03:17:37

您应该使用 AsyncTask 来管理长时间操作。

private class LongOperation extends AsyncTask<HttpResponse, Integer, SomeReturnObject>
{
    ProgressDialog pd;
    long totalSize;

    @Override
    protected void onPreExecute()
    {
        pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("Please wait...");
        pd.setCancelable(false);
        pd.show();
    }

    @Override
    protected SomeReturnObject doInBackground(HttpResponse... arg0)
    {
        // Do long running operation here           
    }

    @Override
    protected void onProgressUpdate(Integer... progress)
    {
        // If you have a long running process that has a progress 
        pd.setProgress((int) (progress[0]));
    }

    @Override
    protected void onPostExecute(SomeReturnObject o)
    {
        pd.dismiss();
    }
}

You should use AsyncTask to manage the long operation.

private class LongOperation extends AsyncTask<HttpResponse, Integer, SomeReturnObject>
{
    ProgressDialog pd;
    long totalSize;

    @Override
    protected void onPreExecute()
    {
        pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("Please wait...");
        pd.setCancelable(false);
        pd.show();
    }

    @Override
    protected SomeReturnObject doInBackground(HttpResponse... arg0)
    {
        // Do long running operation here           
    }

    @Override
    protected void onProgressUpdate(Integer... progress)
    {
        // If you have a long running process that has a progress 
        pd.setProgress((int) (progress[0]));
    }

    @Override
    protected void onPostExecute(SomeReturnObject o)
    {
        pd.dismiss();
    }
}
划一舟意中人 2025-01-14 03:17:37

从上面的代码来看,它实际上是在 Thread run() 方法中显示对话框并立即关闭它。如果你真的想看看它是否显示放置 Thread.sleep(2000) 来测试,但是约翰·拉塞尔所说的是使用 AsyncTask 的方法。

From the above code, it's actually showing the dialog and closing it immediately in the Thread run() method. If you really want to see if it shows put a Thread.sleep(2000) to test, but yes what John Russell said is the way to go to use an AsyncTask instead.

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