Adil Malik
Adil Malik

Reputation: 6357

'If' condition is true but 'else' is being executed

The if condition is true but the code in else snippet gets executed.

enter image description here

I have already tried restarting the Eclipse, it didn't help.

Upvotes: 0

Views: 364

Answers (2)

Adil Malik
Adil Malik

Reputation: 6357

It seems there are two problems.

Problem 1:

First problem was with getCurrentPosition() method of the MediaPlayer

mediaPlayer.stop();
mediaPlayer.getCurrentPosition(); // The current position in milliseconds

In the above code, the second line returns 8549 while the mediaPlayer was stopped already. I was asuming that after stoping the media player, the getCurrentPosition method will return zero milliseconds.

Problem 2:

The second problem (which is actually the answer to this post) was with the debugger. It seems its a bug in debugger.

mPlayer.getCurrentPosition() // value in debugger is 0 (as shown in the screenshot in OP)
mPlayer.getCurrentPosition() == 0 // value in debugger is true (as shown in the screenshot in OP)

But when I log the exact same thing in logcat, it gives different results:

Log.d("pos", mPlayer.getCurrentPosition()+""); // prints a non-zero value in log, in my case 8549
Log.d("con", String.valueOf( mPlayer.getCurrentPosition() == 0 ) ); // prints a false value in log

Which clearly shows why the else code snippet was being executed and why we should not blindly rely on the debugger sometimes :-P

Summary

Whenever you see such dramatic behaviour in debugger, try logging the variables in logcat. (Credit goes to Wakim, because his comment gave me the direction)

Upvotes: 1

Mike
Mike

Reputation: 1343

have you tried

mPlayer.getCurrentPosition().equals("0")

also try placing the codes in if block in else block (vice versa)

Upvotes: 0

Related Questions