Skip to content Skip to sidebar Skip to footer

Service Crashes With Nullpointerexception At Oncreate

i wanted to check the actual time every 5 mins in a service and mute or unmute the phone depending on the time. earlier i tried to use a while(true) with thread.sleep(300000) at th

Solution 1:

TimermyTimer=newTimer();
MyTimerTask myTimerTask= newMyTimerTask();
AudioManager audioManager;

publicvoidonCreate()
{
    super.onCreate();
    audioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
}

you need to initialize audioManager inside onCreate.

Solution 2:

Insted of this:

AudioManageraudioManager= (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

Do this:

AudioManageraudioManager= (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

Post a Comment for "Service Crashes With Nullpointerexception At Oncreate"