I watched weird situation: I didn't get any error when used something like this in my android app code:
@Override
public void onBackPressed() {
if (getActionBar().getSelectedTab().getPosition()==1)**;**
{
if ( getFragmentManager().findFragmentByTag("Tag B") instanceof ContactsArchiveFragment)
{
final ContactsArchiveFragment fragment = (ContactsArchiveFragment) getFragmentManager().findFragmentByTag("Tag B");
if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
Log.i("calls act back cont archive", "on back clicked");
super.onBackPressed();
}
}
}
}
when I tried to do something like this:
@Override
public void onBackPressed() {
if (getActionBar().getSelectedTab().getPosition()==1);
{
if ( getFragmentManager().findFragmentByTag("Tag B") instanceof ContactsArchiveFragment)
{
final ContactsArchiveFragment fragment = (ContactsArchiveFragment) getFragmentManager().findFragmentByTag("Tag B");
if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
Log.i("calls act back cont archive", "on back clicked");
super.onBackPressed();
}
}
}
else
{
}
}
I received Syntax error on token "else", delete this token
. When I saw the semi, I reliazed what is the problem. But this wondered me, can someone explain what it is about?
But this wondered me, can someone explain what it is about?
Sure - the ;
is just an empty statement, and it's fine to have a block with no if
. For example, this is valid:
if (i == 0)
System.out.println("i was 0");
System.out.println("In top-level block");
{
System.out.println("In a block");
}
... and the semi-colon after the if
is just equivalent to the first if
statement with an empty body.
Personally I always use braces for if
statements (and while
statements etc). Some compilers (e.g. the one built into Eclipse) allow you to trip a warning or error if you use an empty statement like this.
The else
form isn't valid because you can only have an else
clause as part of an if
/else
statement, whereas the if
statement is already "done" at the end of the semi-colon.
See more on this question at Stackoverflow