So I have an timer and multiple fragments. If the timer is running, there is no problem when switching between fragments. But when the timer finishes onFinish()
and If the user is in another fragment at that time, the app crashes.
Here are some of the log:
1) E/RingtoneManager: Failed to open ringtone content://settings/system/notification_sound: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
2) java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.Ringtone.setStreamType(int)' on a null object reference
And here is my onFinish()
:
@Override
public void onFinish() {
timerTextView.setText("00:00");
timerSeekBar.setEnabled(true);
Log.i("Timer", "Finished!");
// Alarm sound for ending
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone timerEnded = RingtoneManager.getRingtone(getContext(), notification);
timerEnded.setStreamType(AudioManager.STREAM_ALARM);
timerEnded.play();
}
As far as I understand, the crash is cause when trying to sound the alarm. How can this be fixed?
Edit: Should have clarified that I want the timer to continue running and finish, even when the user in in another fragment.