这是我的代码
public class GameStartActivity extends AppCompatActivity {
TextView timerTV;
Timer20 timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
timerTV = (TextView) findViewById(R.id.timerTV);
timer = new Timer20(20000, 1000);
timer.start();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_start);
}
public class Timer20 extends CountDownTimer {
public Timer20(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
start();
}
@Override
public void onTick(long millisUntilFinished) {
timerTV.setText( "0:" + String.valueOf( millisUntilFinished / 1000 ) );
}
}
}
我在timerTV.setText ......行上得到了这个错误。 :
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'
因此,当我达到这个意图时,它会因此崩溃。
答案 0 :(得分:3)
您需要在setContentView
onCreate
后初始化您的观看次数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_start);
timerTV = (TextView) findViewById(R.id.timerTV);
timer = new Timer20(20000, 1000);
timer.start();
}
答案 1 :(得分:0)
您应该在开头本身初始化onCreate中的视图。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_start);
timerTV = (TextView) findViewById(R.id.timerTV);
timer = new Timer20(20000, 1000);
timer.start();
}