L
L
Lord Drous2016-11-23 21:43:01
Java
Lord Drous, 2016-11-23 21:43:01

When I press the button 2 times, it crashes, how to fix it?

Hello, I have a button that is responsible for stopping the playback of the radio. The problem is that when I click on it 2 times, it crashes out of the application, how can I paint an event or do I need to write some kind of method? Thanks in advance
Button

@Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer = null;
                    playPause.setChecked(false);
                }
                else {
                    playPause.setChecked(true);
                }
            }

I press play, starts playing
20b1ca014e774981adc8d7b0fd2fa56d.png
, press stop, stops
5ee432612e9847e381bab3b8d82fdfd9.png
pressing stop again, does not crash, but returns the pause button
5eb64febee8a490a9b67a0163227b7d2.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2016-11-23
@onepavel

mediaPlayer = null; doesn't bother?

I
ivanessence, 2016-11-28
@ivanessence

Wrap in try/catch and when pressed a second time, display the message "The player has already been stopped"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question