android - 安卓工作室。线程休眠后应用程序崩溃
问题描述
我希望能够在 3 秒后更改文本视图的设置文本。为此,我创建了一个 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);
推荐阅读
- java - 在eclipse中构建我的java应用程序后出错
- reactjs - http://localhost:5000/auth/google/callback 404 错误
- jmeter - 我可以使用 csvdataconfig 中的变量作为 jmeter 目录列表中的源目录吗
- python - 更新谷歌表上的特定标签 - Python
- ckeditor - 如何从外部书签链接跳转到 React Ckeditor 5 段落?
- python - 从 groupby 中的前一个组中获取值
- windows - 在汇编代码中使用 printf 函数
- android - Recyclerview 在滚动上重新创建动态芯片
- r - rowSums 为 0 的子集行
- python - Hackerrank 问题:不知道 -> 在 Python 3 中设置与列出 RUNTIME