I am creating an Android app with Android Studio, however, my app crashes at runtime and I can't figure out why? My code looks fine and it has got no errors. I have an 'if statement' inside a public void onBackPressed().
//stop's the car sound that is currently being played when the devices back button is clicked
@Override
public void onBackPressed() {
if (mySound !=null) {
mySound.stop();
this.finish();
if (mySound == null) {
this.finish();
}
}
mySound is equal to a MediaPlayer.
My first 'if statement' is for when the back button is pressed (while the sound is playing), the sound will stop and the activity will close and go to the previous activity.
And my second 'if statement' is for when the back button is pressed (while the sound is NOT playing), the activity will close and go to the previous activity.
I have tried using || but that doesn't seem to work? Does anyone know what's wrong with my code?
Thanks!
Aucun commentaire:
Enregistrer un commentaire