Seekbar handler Null Exception

In the mainactivity I have create a handler as instance variable:

SeekBar seek_bar;
MediaPlayer player;
Handler seekHandler = new Handler();

Then I have the following two methods in the MainActivity to update the seekbar as the audio plays in media player:

public void getInit() {
    seek_bar = (SeekBar) findViewById(R.id.seek_bar);
}

Runnable run = new Runnable() {

    @Override
    public void run() {
        seekUpdation();  **//Exception comes here while closing the app**
    }
};

public void seekUpdation() {

    seek_bar.setProgress(mMediaPlayer.getCurrentPosition());
    seekHandler.postDelayed(run, 1000);
}

The problem I am facing is that when the audio is running and user closes the application using device back buttton. I get NullPointerException. The destroy method of activity is:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

Upvotes: 0

Views: 122

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93561

Remove any queued messages/callbacks from the handler in your onDestroy. My bet is that its running the last message after onDestroy is called.

Upvotes: 1

Related Questions