我看到了奇怪的情况:在我的 Android 应用程序代码中使用类似这样的东西时我没有收到任何错误:
@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();
}
}
}
}
当我尝试做这样的事情时:
@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
{
}
}
我收到 token “else”的语法错误,删除此 token
。当我看到半成品时,我才意识到问题出在哪里。但这让我感到疑惑,有人可以解释一下它是关于什么的吗?
最佳答案
But this wondered me, can someone explain what it is about?
当然 - ;
只是一个空语句,有一个没有 if
的 block 也没问题。例如,这是有效的:
if (i == 0)
System.out.println("i was 0");
System.out.println("In top-level block");
{
System.out.println("In a block");
}
... if
之后的分号等同于第一个带有空主体的 if
语句。
我个人总是对if
语句(和while
语句等)使用大括号。如果您使用像这样的空语句,一些编译器(例如 Eclipse 中内置的编译器)允许您触发警告或错误。
else
形式无效,因为您只能将 else
子句作为 if
/else< 的一部分
语句,而 if
语句在分号末尾已经“完成”。
关于java - 机器人 : if(); do not caused any prohibition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18876724/