我想按顺序显示一些单词(字符串),每个单词一段时间,我正在使用这段代码,但在显示所有单词后,应用程序崩溃,我希望它在所有单词都显示后返回到上一页显示,请帮忙,提前致谢。
final TextView textView=(TextView)findViewById(R.id.textid);
Thread t=new Thread(){
@Override
public void run(){
while(!isInterrupted()){
try {
Thread.sleep(1000); //1000ms = 1 sec
runOnUiThread(new Runnable() {
@Override
public void run() {
count++;
textView.setText(words[count]);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
最佳答案
您没有检查计数是否在字数范围内。
你需要改变
count++;
textView.setText(words[count]);
至
count++
if (count < words.length){
textView.setText(words[count]);
}else{
// Assuming you are inside an activity called MyActivity
MyActivity.this.finish();
}
关于java - 使用线程按顺序显示字符串后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57380598/