2014-02-05 33 views
1

我試圖找到答案......,而且不知道我在做什麼錯顯示java.lang.NullPointerException我的服務媒體播放器

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

當我開始練習我運行的服務,其中i我正在播放音樂。音樂響起罰款,但是當我點擊暫停或靜音按鈕,空指針異常的mp.pause)應用程序崩潰(或pm.volume(0,0)或mp.stop 活動,這裏的代碼:

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

RadioService的第106行是什麼? – Aashir

+0

你如何實例化你的服務?直接在服務上調用方法表明你沒有正確執行。 – laalto

+0

on line 106我有 nm.cancelAll(); – Alexandro

回答

0

我覺得你varible 「納米」 正在成爲空。所以請確認它發生在哪裏。

+0

但它寫入mp.pause()中的null不在通知中 – Alexandro

相關問題