我试图做其他事情,但继续删除代码,认为简化事情将有助于识别我的错误。这就是我剩下的(经过大量简化),但我仍然不明白为什么它不应该这样做!
public class MyOwnTimerActivity extends Activity {
int timeLeft = 5;
TextView timerTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button timerButton = (Button) findViewById(R.id.button1);
timerTextView = (TextView) findViewById(R.id.timerText);
Thread timer = new Thread() {
public void run() {
try {
sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally{
timerTextView.setText("something");
}
}
};
timer.start();
}
}
错误:应用程序在 3 秒后崩溃,即在计时器关闭后。