方向上的动态时间显示崩溃 [英] Dynamic time display crash on orientation
本文介绍了方向上的动态时间显示崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个布局,将显示一个 TextView
用于显示滴答时间.我按照此链接中的代码进行操作
I have a layout that will display a TextView
which is used to display a ticking time.I followed the codes from this link
但我得到一个错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我在这里遇到了同样的问题,但我解决了它
I had the same problem here but I fixed it
Intent extras null on configuration change
这里是 Java 代码
Here are the Java codes
void clockTicking(){
final CountDownTimer newtimer = new CountDownTimer(1000000000, 1000) {
public void onTick(long millisUntilFinished) {
timeDisplay = (TextView)findViewById(R.id.txtTime);
Calendar c = Calendar.getInstance();
timeDisplay.setText(c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND)+" PM");
}
public void onFinish() {
}
};
newtimer.start();
推荐答案
在你的代码中 timeDisplay 对象为空.
In your code timeDisplay object is null.
确保您的 textview id 正确.仔细检查这一行 timeDisplay = (TextView)findViewById(R.id.txtTime);
Make sure your textview id is correct. Double check this line timeDisplay = (TextView)findViewById(R.id.txtTime);
我认为您的 id txtTime 不正确.
I think your id txtTime is incorrect.
希望对您有所帮助.
这篇关于方向上的动态时间显示崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文