[英]Android Studio. App crashes after thread sleeps
I want to be able to change the set text of a Text View after 3 seconds have passed.我希望能够在 3 秒后更改文本视图的设置文本。 To do this I have created a try catch statement and have told the thread to sleep for 3000 milliseconds before the code below is executed.为此,我创建了一个 try catch 语句,并告诉线程在执行下面的代码之前休眠 3000 毫秒。 Unfortunately when I run the app the program simply waits for 3 seconds and then crashes.不幸的是,当我运行该应用程序时,程序只是等待 3 秒然后崩溃。 Any help would be greatly appreciated.任何帮助将不胜感激。
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");
}
};
I use postDelayed() for this kind of tasks我将 postDelayed() 用于此类任务
sup_txt3.postDelayed(() -> sup_txt3.setText("I am sensing the letter A", 3000);
I shorted it, the complete code would be我把它做短了,完整的代码是
sup_txt3.postDelayed(new Runnable() {
@Override
public void run() {
sup_txt3.setText("I am sensing the letter A");
}
}, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.