1

我正在测试简单的警报应用程序。在第一个活动中,我使用从 Ringtonemanger 挑选的铃声设置警报,为此我使用了 setRing()方法。然后通过意图我将它传递给广播接收器,但是当在特定时间调用警报时,然后在player.setAudioStreamType(AudioManager.STREAM_ALARM) ** 它抛出 **NULL 指针异常。有人知道吗?

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);               
         }

     }

  }
}
4

1 回答 1

1

你永远不会初始化你的player变量。它在广播接收器的整个存在期间为空。因此,当您调用setAudioStreamType()它时,您会得到空指针异常。您不会player在广播接收器的其他任何地方使用该变量,我认为您实际上是打算在 mMediaPlayer 的 if 块中调用所有这些方法。因此,您可以通过调用该对象上的所有方法来解决您的问题,如下所示:

 if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
  }
于 2011-11-01T15:01:11.063 回答