ProgressDialog 不显示。再次
我很困惑,因为这在其他活动中对我来说非常有效,在这里我基本上只是复制粘贴代码,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定这是否是问题的根本原因,但请尝试执行 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.
您应该使用 AsyncTask 来管理长时间操作。
You should use AsyncTask to manage the long operation.
从上面的代码来看,它实际上是在 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.