Ternary operator not working in Android
I have a simple question that boggles me. I am trying to use the ternary operator in java. I am new to Android and java. This code gives me the error:
amt < 0 ? lendBtn.setChecked(true) : lendBtn.setChecked(false);
"Syntax error on token 开发者_如何学Python"<", invalid AssignmentOperator"
So, I replace it with an if statement and it totally works:
if (amt < 0) { ... }
It's not a big deal but does anyone know why?
This has nothing to do with Android. You can't use a conditional expression as a statement on its own... and the second and third operands can't be void expressions either.
You should use:
lendBtn.setChecked(amt < 0);
... which is simpler to start with.
精彩评论