Android wait() 不工作

发布于 2024-12-13 10:00:08 字数 518 浏览 4 评论 0原文

这是我的第一个问题,如果我违反了论坛规则,请原谅我。

我需要我的程序等待 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 技术交流群。

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

发布评论

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

评论(5

后eg是否自 2024-12-20 10:00:08

正如人们所指出的,不要休眠或以其他方式阻塞 UI 线程,但您似乎在创建的新线程中意识到了这一点。

现在为什么它的行为不符合您的要求:

现在,您调用 setContentView(),启动一个新线程,调用 setContentView() > 在UI线程上第二次——这会快速连续发生,中间没有延迟/等待/睡眠。 wait(3000) 发生在您启动的新线程中 - 因此该新线程启动,等待 3000 毫秒,然后退出。这是一个昂贵的无操作。

您需要从新线程的 run() 方法内部第二次调用 setContentView() 才能获得所需的效果。另外,您应该使用 sleep() 而不是 wait() —— wait() 是一个低级工具,用于同步线程,而< code>sleep() 是通常的“不要继续 X 时间”。

让我提出一个更好的方法:

一个可以说更好、更轻量级的方法是使用 Handler.postDelayed() -- 这允许您在 UI 上调用 Runnable延迟后的线程:

setContentView(R.layout.logout);
new Handler().postDelayed(new Runnable() {
    @Override
        public void run() {
            setContentView(R.layout.main);
        }
    }, 3000);

编辑以回复您的评论:

定义并找到 Runnable 之前的按钮作为final变量,这样您就可以稍后访问它从 Runnable 内部。

请注意,要从匿名内部类(您的 new Runnable())内部引用周围类的 this 实例,您需要在其前面添加类名前缀周围的类(您的 Activity 类):

final View submitButton = findViewById(R.id.submit_button); 
setContentView(R.layout.logout);
new Handler().postDelayed(new Runnable() {
    @Override
        public void run() {
            setContentView(R.layout.main);
            // adapt to your actual Activity class name:
            submitButton.setOnClickListener(YourClassName.this);
        }
    }, 3000);

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, call setContentView() a second time on the UI thread -- this happens in quick succession with no delay/wait/sleep inbetween. The wait(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's run() method to get the desired effect. Also, you should use sleep() instead of wait() -- wait() is a low-level tool for synchronizing threads while sleep() 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:

setContentView(R.layout.logout);
new Handler().postDelayed(new Runnable() {
    @Override
        public void run() {
            setContentView(R.layout.main);
        }
    }, 3000);

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 (your new Runnable()), you need to prefix it with the class name of the surrounding class (your Activity class):

final View submitButton = findViewById(R.id.submit_button); 
setContentView(R.layout.logout);
new Handler().postDelayed(new Runnable() {
    @Override
        public void run() {
            setContentView(R.layout.main);
            // adapt to your actual Activity class name:
            submitButton.setOnClickListener(YourClassName.this);
        }
    }, 3000);
玩世 2024-12-20 10:00:08

wait() 不会等待一定的时间,而是让当前线程等待 this 执行 notify()最长的时间。您正在寻找的是Thread.sleep()

目前唯一等待的是您正在生成的附加线程,而不是活动本身。这就是为什么我建议您查看 Handler.postDelayed(), CountDownTimerAsyncTask。处理线程是非常低级的。

wait() doesn't wait for a certain amount of time, but rather has the current Thread wait for this to do a notify() for a maximum amount of time. What you are looking for, is Thread.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 or AsyncTask. Handling threads is very low-level.

回首观望 2024-12-20 10:00:08

您的代码无法睡眠 UI 线程。要睡眠 UI 线程,请尝试此代码

new Handler().postDelayed(new Runnable() 
{ 
    public void run() 
    { 
        setContentView(R.layout.main);
    }
}, 3000);

Your code not work to sleep UI thread.To sleep UI thread try this code

new Handler().postDelayed(new Runnable() 
{ 
    public void run() 
    { 
        setContentView(R.layout.main);
    }
}, 3000);
终遇你 2024-12-20 10:00:08

尝试使用 Sleep() 而不是 Wait()

android.os.SystemClock.sleep(3000)

Try using Sleep() instead of Wait()

android.os.SystemClock.sleep(3000)

夏末 2024-12-20 10:00:08

据我所知,等待发生在新线程中,当您在当前线程中调用 setContentView(R.layout.main) 时。

请注意

setContentView(..)
synchronized(this) {
    this.wait(1000);
}
setContentView(..)

,在 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

setContentView(..)
synchronized(this) {
    this.wait(1000);
}
setContentView(..)

Please note sleeping or waiting in the UI thread is not a best practice though.

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