点按“返回”按钮时,MediaPlayer会收到Null Pointer Exception

时间:2014-11-01 05:03:44

标签: java android media-player android-mediaplayer

Streaming Audio在线,但总是得到 NullPointerException ,只要点击BACK按钮(playing music时),

这是line,我在UpdateSongTime 可运行中获得异常:

  startTime = mediaPlayer.getCurrentPosition(); // NullPointerException

代码如下所示:

        public TextView startTimeField,endTimeField;
        private double startTime = 0;
        private double finalTime = 0;
        private SeekBar seekbar;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_music_player);

          public void play(View view){         
           mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(videoURL);
            } catch (IllegalArgumentException e) {
            } catch (SecurityException e) {
            } catch (IllegalStateException e) {
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mediaPlayer.prepare();
            } catch (IllegalStateException e) {
            } catch (IOException e) {
            }
            mediaPlayer.start();

            finalTime = mediaPlayer.getDuration();
              startTime = mediaPlayer.getCurrentPosition();
              if(oneTimeOnly == 0){
                 seekbar.setMax((int) finalTime);
                 oneTimeOnly = 1;
              } 

              endTimeField.setText(String.format("%dm:%ds", 
                 TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
                 TimeUnit.MILLISECONDS.toSeconds((long) finalTime) - 
                 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                 toMinutes((long) finalTime)))
              );
              startTimeField.setText(String.format("%dm:%ds", 
                 TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                 TimeUnit.MILLISECONDS.toSeconds((long) startTime) - 
                 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                 toMinutes((long) startTime)))
              );
              seekbar.setProgress((int)startTime);
              myHandler.postDelayed(UpdateSongTime,100); 


          pauseButton.setEnabled(true);
          playButton.setEnabled(false);

          mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                pauseButton.setEnabled(false);
                  playButton.setEnabled(true);
                  startTimeField.setText("0m:0s");
                  seekbar.setProgress(0);
            }
        });
       }

       private Runnable UpdateSongTime = new Runnable() {
              public void run() {
                 startTime = mediaPlayer.getCurrentPosition(); // NullPointerException

                 startTimeField.setText(String.format("%dm:%ds", 
                    TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                    TimeUnit.MILLISECONDS.toSeconds((long) startTime) - 
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                    toMinutes((long) startTime)))
                 );
                 seekbar.setProgress((int)startTime);
                 myHandler.postDelayed(this, 100);
              }
           };

 public void pause(View view){       
          mediaPlayer.pause();
          pauseButton.setEnabled(false);
          playButton.setEnabled(true);
       }    

/** Called just before the activity is destroyed. */
        @Override
        public void onDestroy() {
           super.onDestroy();

               if(mediaPlayer!=null && mediaPlayer.isPlaying()){
                   mediaPlayer.stop();
                   mediaPlayer.release();
                   mediaPlayer = null;
                }         
        }

我的代码中没有实现的内容?它需要更改我的代码?

1 个答案:

答案 0 :(得分:1)

您遗失了Handler <{1}} onDestroy()中的Activity回调内容

onDestroy()方法

中添加此行
myHandler.removeCallbacks(this);