gpt4 book ai didi

android - Android Studio。线程休眠后应用崩溃

转载 作者:行者123 更新时间:2023-12-02 07:19:03 25 4
gpt4 key购买 nike

我希望能够在3秒钟后更改“文本 View ”的设置文本。为此,我创建了一条try catch语句,并告诉线程在执行以下代码之前先休眠3000毫秒。不幸的是,当我运行该应用程序时,该程序仅等待3秒钟,然后崩溃。任何帮助将不胜感激。

    Runnable runnable3 = new Runnable() {

@Override
public void run() {
TextView sup_txt3 = findViewById(R.id.sup_txt3);
sup_txt3.setText("Ooooohhhh this is very hard. I am receiving many thoughts");


try{
Thread.sleep(3000);
}catch(Exception e){

}
sup_txt3.setText("I am sensing the letter A");

}
};

最佳答案

我将postDelayed()用于此类任务

sup_txt3.postDelayed(() -> sup_txt3.setText("I am sensing the letter A", 3000);
我做短了,完整的代码是
sup_txt3.postDelayed(new Runnable() {
@Override
public void run() {
sup_txt3.setText("I am sensing the letter A");
}
}, 3000);

关于android - Android Studio。线程休眠后应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63050611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com