Giving Null pointer exception at the invoke time of alarm which is already set in prevoius activity

StackOverflow https://stackoverflow.com/questions/7968345

  •  18-02-2021
  •  | 
  •  

Вопрос

I am testing simple alarm application. In firstactivity I set alarm with ringtone which is picked from Ringtonemanger, for that I used setRing() method. Then through intent I passed it to Broadcast reciever,But when alarm invoked at specific time then at player.setAudioStreamType(AudioManager.STREAM_ALARM) ** it throws **NULL pointer exception .Anyone has idea??

private void setRing()
{
    intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,  RingtoneManager.TYPE_ALL);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);

    this.startActivityForResult(intent, 5);

}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
{
    if (resultCode == Activity.RESULT_OK && requestCode == 5)
    {
         Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

         if (uri != null)
         {
             this.chosenRingtone = uri.toString();
         }
         else
         {
             this.chosenRingtone = null;
         }

     }            




public class GroupsCheckAlarmReceiver extends BroadcastReceiver {    

 MediaPlayer mMediaPlayer ;
 MediaPlayer player;
 Context context1;
 Uri uri=null;

@Override
public void onReceive(final Context context, Intent intent) {
     Toast.makeText(context, "Alarm success", Toast.LENGTH_LONG).show();
     String struri=intent.getStringExtra("uristr");
     Log.v("value of ring",struri);
     uri=Uri.parse(struri);
     context1=context;
     try {
        callringtone();
    } catch (IllegalArgumentException e) {

        e.printStackTrace();
    } catch (SecurityException e) {

        e.printStackTrace();
    } catch (IllegalStateException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

}
private void callringtone() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
     mMediaPlayer = new MediaPlayer();
     mMediaPlayer.setDataSource(context1,uri);

     final AudioManager audioManager = (AudioManager)context1.getSystemService(Context.AUDIO_SERVICE);

     if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                player.setAudioStreamType(AudioManager.STREAM_ALARM);
                player.setLooping(true);
                player.prepare();
                player.start();
      }

     if(uri == null){

         uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         if(uri== null){ 

             uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
         }

     }

  }
}
Это было полезно?

Решение

You never initialize your player variable. It's null throughout the entire existence of your Broadcast receiver. So when you call setAudioStreamType() on it, you get your null pointer exception. You don't use the player variable anywhere else in your broadcast receiver, and I think you actually meant to call all those methods in your if block on mMediaPlayer. So you could solve your problem by calling all your methods on that object instead, like so:

 if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top