我的服务媒体播放器上的java.lang.NullPointerException

时间:2014-02-05 10:27:33

标签: android android-service android-mediaplayer

我试图找到答案......并且不知道我做错了什么

public class RadioService extends Service {NotificationManager nm;Intent i;
String artist, title, streamUrl, title_play;
int id = 0;
public MediaPlayer mp ;

@Override
public void onCreate() {
    super.onCreate();
    mp = new MediaPlayer();
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mp.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
             mp.start(); // TODO Auto-generated method stub

        }
    });





}
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {

    try {
        artist = intent.getStringExtra("artist");
        title = intent.getStringExtra("title");
        streamUrl = intent.getStringExtra("streamUrl");
        title_play = intent.getStringExtra("title_play");
        id = intent.getIntExtra("id", 0);
        Log.d("MyLog in radioservise", "" + artist + title + streamUrl
                + title_play + id);

    } catch (Exception e1) {

        Log.d("MyLog", "" + artist + title + streamUrl + title_play + id);
        Log.d("Exeption here", e1 + "");
        e1.printStackTrace();
    }
    Play();

    sendNotif();
    return super.onStartCommand(intent, flags, startId);
}

void sendNotif() {

    Intent intent = new Intent(this, PlayActivity.class);
    // intent.putExtra(MainActivity.FILE_NAME, "somefile");
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);


    notif.setLatestEventInfo(this, title_play, streamUrl, pIntent);


    notif.flags |= Notification.FLAG_AUTO_CANCEL;


    nm.notify(1, notif);

}

public void onExit() {

      if (mp != null)
        {
              mp.stop();
          mp.release();
          mp = null;
        }
    nm.cancelAll();
}



public void Play(/* String play */) {

    try {
        if (mp.isPlaying()) {
            stop();
        } else {
            mp.reset();
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setDataSource(streamUrl);
            mp.prepareAsync();

        }
    } catch (Exception e) {
    }}

public void Pause() {
    try {
        if (mp.isPlaying()) {
            if (mp!=null){
        mp.pause();

        }
        }
    } catch ( Exception e) {

        e.printStackTrace();
    }
}

public void Mute() {
    try {
        mp.setVolume(0, 0);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void Volume() {
    try {
        mp.setVolume(1, 1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void stop() {
    if (mp.isPlaying()) {

        try {
            mp.stop();
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

当我启动我运行服务的活动时,我正在播放音乐。音乐播放正常,但是当我点击暂停或静音按钮时,应用程序崩溃,在mp.pause()或pm.volume(0,0)或mp.stop上出现NullPointer异常 这里的活动代码:

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.im_play:
        rServ.Play(/*streamUrl*/);
        ResetInfo(streamUrl);
        break;
    case R.id.im_pause:
        rServ.Pause();
        // rServ.mp.pause();
        break;
    case R.id.im_mute:

        if (z == true) {
            rServ.Mute();
            mut.setImageResource(R.drawable.no_mute_bt_ic);
            z = false;
        } else {
            rServ.Volume();
            mut.setImageResource(R.drawable.mute_bt_ic);
            z = true;
        }
        break;
    }
}

public void onBackPressed() {
    tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

    try {
        //if (rServ.mp != null && rServ.mp.isPlaying()) {
            rServ.onExit();
            //rServ.stop();
            t.cancel();
        //}
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

Logcat错误:

> 02-05 10:01:04.238: E/AndroidRuntime(429): FATAL EXCEPTION: main
02-05 10:01:04.238: E/AndroidRuntime(429): java.lang.NullPointerException
02-05 10:01:04.238: E/AndroidRuntime(429):  at com.example.smpleradio.RadioService.onExit(RadioService.java:106)
02-05 10:01:04.238: E/AndroidRuntime(429):  at com.example.smpleradio.PlayActivity.onBackPressed(PlayActivity.java:190)
02-05 10:01:04.238: E/AndroidRuntime(429):  at com.example.smpleradio.PlayActivity.onOptionsItemSelected(PlayActivity.java:249)

1 个答案:

答案 0 :(得分:0)

我认为你的变量“nm”变为空。所以请验证它发生的地方。