0

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.

Zeo
  • 99
  • 1
  • 11

2 Answers2

0

Modify it like this:

 @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(getApplicationContext(), notification);
                        timerEnded.setStreamType(AudioManager.STREAM_ALARM); 
                        timerEnded.play();

                    }
Avinash Roy
  • 953
  • 1
  • 8
  • 25
  • I should have mentioned, that I only have one activity. And If I'm going by this solution, I have to use getActivity().getApplicationContext(), otherwise the single getApplicationContext() wont work. – Zeo May 17 '17 at 20:01
  • i was not aware that u were using a fragment – Avinash Roy May 17 '17 at 20:03
  • Quite sadly, it did not. I have one activity and multiple fragments. IF im out of this timer fragment, when the timer finished, it will crash. – Zeo May 17 '17 at 20:05
0

If you want your timer to finish when user leave fragment then you can use onstop and stop timer there.

Or if you want your timer to pause for when user comes back you can store rest of the seconds and start timer for that.

According to your situation I think you should read this post and go with asynctask.

https://stackoverflow.com/a/14611934/5308202

Check before accessing ui from asynctask.

Community
  • 1
  • 1
mnp343
  • 329
  • 1
  • 6
  • Should have clarified that I want the timer to continue running and finish, even when the user in in another fragment. – Zeo May 17 '17 at 20:20