My timer app is crashing as soon as countDownTimer.cancel()
method is called it is showing null object reference even though I have done it correctly, I can't find the problem. Please, someone, take a look on the code below: (also I am attaching logcat)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
package com.example.rajdeepkgautam.itimer;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int max = 600;
int start = 30;
TextView timerTextView;
SeekBar timerSeekBar;
boolean timerActive = false;
Button runButton;
CountDownTimer countDownTimer;
public void resetTimer() {
timerTextView.setText("0:30");
timerSeekBar.setProgress(start);
timerSeekBar.setEnabled(true);
countDownTimer.cancel();
runButton.setText("RUN!");
timerActive = false;
}
public void buttonClicked(View view) {
if(timerActive) {
resetTimer();
} else {
timerActive = true;
runButton.setText("STOP!");
timerSeekBar.setEnabled(false);
CountDownTimer countDownTimer = new CountDownTimer((timerSeekBar.getProgress()*1000) + 100, 1000) {
@Override
public void onTick(long l) {
TimerUpdate((int) l/1000);
}
@Override
public void onFinish() {
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.horn);
mediaPlayer.start();
resetTimer();
}
}.start();
}
}
public void TimerUpdate(int TimeLeft) {
int minutes = TimeLeft/60;
int seconds = TimeLeft - (minutes*60);
if(seconds <=9) {
timerTextView.setText(Integer.toString(minutes) + ":" + "0" + Integer.toString(seconds));
} else {
timerTextView.setText(Integer.toString(minutes) + ":" + Integer.toString(seconds));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerSeekBar = findViewById(R.id.timerSeekBar);
timerTextView = findViewById(R.id.timerTextView);
runButton = findViewById(R.id.runButton);
timerSeekBar.setMax(max);
timerSeekBar.setProgress(start);
timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
TimerUpdate(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
It's because of not initializing the countDownTimer
. You are creating a local variable and never assigning the instantiated CountDownTimer
to the countDownTimer
of MainActivity
.
Change the line:
CountDownTimer countDownTimer = new CountDownTimer((timerSeekBar.getProgress()*1000) + 100, 1000)
to:
countDownTimer = new CountDownTimer((timerSeekBar.getProgress()*1000) + 100, 1000)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.