Android wait() 不工作
这是我的第一个问题,如果我违反了论坛规则,请原谅我。
我需要我的程序等待 3 秒钟,然后更改内容视图
我的代码是:
setContentView(R.layout.logout);
new Thread(new Runnable() {
@Override
public void run(){
try {
synchronized(this){
wait(3000);
}
}
catch(InterruptedException ex){
}
}
}).start();
setContentView(R.layout.main);
程序运行时没有错误,但没有等待。当到达这部分时,它只显示“主”布局,根本不显示“注销”布局。
我的方法有什么问题?
This is my first question here, so please forgive me if I disobeyed any of the forum rules.
I need my program to wait for 3 seconds and then change the Content View
My code is:
setContentView(R.layout.logout);
new Thread(new Runnable() {
@Override
public void run(){
try {
synchronized(this){
wait(3000);
}
}
catch(InterruptedException ex){
}
}
}).start();
setContentView(R.layout.main);
The program works with no errors, but there is no waiting. When it gets to this part, it just shows the "main" layout without showing the "logout" layout at all.
What is wrong in my approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如人们所指出的,不要休眠或以其他方式阻塞 UI 线程,但您似乎在创建的新线程中意识到了这一点。
现在为什么它的行为不符合您的要求:
现在,您调用
setContentView()
,启动一个新线程,调用setContentView()
> 在UI线程上第二次——这会快速连续发生,中间没有延迟/等待/睡眠。wait(3000)
发生在您启动的新线程中 - 因此该新线程启动,等待 3000 毫秒,然后退出。这是一个昂贵的无操作。您需要从新线程的
run()
方法内部第二次调用setContentView()
才能获得所需的效果。另外,您应该使用sleep()
而不是wait()
——wait()
是一个低级工具,用于同步线程,而< code>sleep() 是通常的“不要继续 X 时间”。让我提出一个更好的方法:
一个可以说更好、更轻量级的方法是使用
Handler.postDelayed()
-- 这允许您在 UI 上调用 Runnable延迟后的线程:编辑以回复您的评论:
定义并找到 Runnable 之前的按钮作为final变量,这样您就可以稍后访问它从 Runnable 内部。
请注意,要从匿名内部类(您的
new Runnable()
)内部引用周围类的this
实例,您需要在其前面添加类名前缀周围的类(您的 Activity 类):As people noted, don't sleep on or otherwise block the UI thread, but you seem to be aware of this with the new thread you create.
Now for why it doesn't behave as you want:
Right now, you call
setContentView()
, start a new thread, callsetContentView()
a second time on the UI thread -- this happens in quick succession with no delay/wait/sleep inbetween. Thewait(3000)
happens in the new thread you started -- so that new thread starts, waits for 3000ms, then exits. It's an expensive no-op.You would need to make the second call to
setContentView()
from inside that new thread'srun()
method to get the desired effect. Also, you should usesleep()
instead ofwait()
--wait()
is a low-level tool for synchronizing threads whilesleep()
is the usual "don't continue for X amount of time".Let me propose a better way:
An arguably nicer and much lighter approch is using
Handler.postDelayed()
-- this allows you to invoke a Runnable on the UI thread after a delay:Edit to reply to your comment:
Define and find the button before the Runnable as a final variable, this way you can access it later from inside the Runnable.
Please note that to reference the
this
instance of the surrounding class from inside an anonymous inner class (yournew Runnable()
), you need to prefix it with the class name of the surrounding class (yourActivity
class):wait()
不会等待一定的时间,而是让当前线程等待this
执行notify()
最长的时间。您正在寻找的是Thread.sleep()
。目前唯一等待的是您正在生成的附加线程,而不是活动本身。这就是为什么我建议您查看
Handler.postDelayed()
,CountDownTimer
或AsyncTask
。处理线程是非常低级的。wait()
doesn't wait for a certain amount of time, but rather has the current Thread wait forthis
to do anotify()
for a maximum amount of time. What you are looking for, isThread.sleep()
.And at the moment the only thing that will be waiting, is the additional thread you are spawning, not the activity itself. That's why I'd suggest you look at
Handler.postDelayed()
,CountDownTimer
orAsyncTask
. Handling threads is very low-level.您的代码无法睡眠 UI 线程。要睡眠 UI 线程,请尝试此代码
Your code not work to sleep UI thread.To sleep UI thread try this code
尝试使用 Sleep() 而不是 Wait()
Try using Sleep() instead of Wait()
据我所知,等待发生在新线程中,当您在当前线程中调用 setContentView(R.layout.main) 时。
请注意
,在 UI 线程中休眠或等待并不是最佳实践。
As far as i can understand the wait is happening in the new thread where as you are calling setContentView(R.layout.main) in the current thread.
Try
Please note sleeping or waiting in the UI thread is not a best practice though.