English 中文(简体)
援引警报时给予Null Pointer以例外处理,这种例外情况已在事前的活动中作出规定。
原标题:Giving Null pointer exception at the invoke time of alarm which is already set in prevoius activity

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

     }

  }
}
最佳回答

页: 1 整个广播接收者的存在都是无效的。 因此,当您打电话setAudioStreamType()时,你就获得你的无意义例外。 你不使用<条码>流星在任何其他地方的广播接收器,我认为,如果在小媒体播放台上封幕,你实际上打算把所有这些方法放在你身上。 因此,你可以像你一样,用一切方法解决你的问题:

 if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
  }
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签