动态时间显示在方向上崩溃 [英] Dynamic time display crash on orientation

查看:25
本文介绍了动态时间显示在方向上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,将显示一个 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

如何显示android中每秒动态变化的当前时间

但我得到一个错误

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 配置更改

这里是Java代码

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆