当数字时钟每分钟刷新一次格式的时间时,我想每分钟刷新一个 TextView 的文本hh/mm
。我在Activity中放置了一个名为txtView1的TextView并创建了一个类Digital Clock。当我运行应用程序时,应用程序退出并出错。我真的不知道为什么这里是onAttachedToWindow()
关于Digital Clock的重要功能:
protected void onAttachedToWindow() {
mTickerStopped = false;
super.onAttachedToWindow();
mHandler = new Handler();
/**
* requests a tick on the next hard-second boundary
*/
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
String content = (String) DateFormat.format(mFormat, mCalendar);
if(content.split(" ").length > 1){
content = content.split(" ")[0] + content.split(" ")[1];
}
setText(android.text.Html.fromHtml(content));
//-----Here is the TextView I want to refresh
TextView txtV1 = (TextView)findViewById(R.id.txtView1);
txtV1.setText("Now Fresh");//Just for try,so set a constant string
invalidate();
long now = SystemClock.uptimeMillis();
//refresh each minute
long next = now + (60*1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();
}