【问题标题】:Syntax error on token "}" error appears [closed]出现令牌“}”错误的语法错误[关闭]
【发布时间】:2014-01-10 01:20:49
【问题描述】:

我收到此错误“令牌“}”的语法错误,请删除此令牌。”在最后一行,我不知道为什么,因为我是 Android 新手。

   public void ok() throws UiObjectNotFoundException{
            //trying to find ok
                   try{
                       UiObject okDialog = new UiObject(new UiSelector().text("ok"));
                   if(okDialog.exists()){
                       System.out.println("Success");   
                       break;//if ok exists it should come out
                   }
                   //if ok not exists
                   if(!okDialog.exists()){
                         System.out.println("retrying");
                         getUiDevice().pressHome();
                         UiObject enterButton = new UiObject(new UiSelector().text("enter"));
                         enterButton.clickAndWaitForNewWindow();  
                   }
                   //validating again to check ok exists
                   if(okDialog.exists())
                         break;
                   else   //if ok not exists again printing the belwo message
                         System.out.println("unable to find"); 
                         getUiDevice().pressHome();
    //Here if ok not exists after going to home, i want to stop the whole script here..how to do this?
                   }catch (UiObjectNotFoundException e) {}

                   }
    } Error message appears here : Syntax error on token "}", delete this token

【问题讨论】:

  • 你是开发者???
  • 从您的代码中删除最后一个“}”。或者完美地检查你的整个班级。
  • 我刚开始学习。
  • @CapDroid 你能回答我这个问题吗? stackoverflow.com/questions/20715655/…
  • @user3089474 , if条件不能加break,需要return而不是break

标签: android eclipse syntax token


【解决方案1】:

如果您只知道英语知识,那么您可以在显示错误消息的一侧轻松删除此 }

if条件中不能加break,需要return而不是break,if条件中去掉{}

} Error message appears here : Syntax error on token "}", delete this token 

复制过去的代码而不是你的方法

  public void ok() throws  UiObjectNotFoundException
{
        //trying to find ok
               try{

                   UiObject okDialog = new UiObject(new UiSelector().text("ok"));

                   if(okDialog.exists())
                   System.out.println("Success");   
                   return;//if ok exists it should come out


               //if ok not exists
               if(!okDialog.exists())
                     System.out.println("retrying");
                     getUiDevice().pressHome();
                     UiObject enterButton = new UiObject(new UiSelector().text("enter"));
                     enterButton.clickAndWaitForNewWindow();  


               //validating again to check ok exists
               if(okDialog.exists())
                     return;
               else   //if ok not exists again printing the belwo message
                     System.out.println("unable to find"); 
                     getUiDevice().pressHome();
//Here if ok not exists after going to home, i want to stop the whole script here..how to do this?
               }
catch (UiObjectNotFoundExceptione) {}

               }

【讨论】:

  • 对于方法签名,使用 throws 而不是 throw。 public void ok() 抛出 UiObjectNotFoundException 是正确的
  • 是的,对此感到抱歉\
  • 你不能使用break in if条件。
  • 我的答案更新了,请看一下
  • 嘿 Ramesh Rajendran,它工作正常:)....我想在“再次验证以检查是否存在”时停止脚本,它应该回家然后脚本不应该继续进行。跨度>
【解决方案2】:

您的代码中的某些地方可能缺少右括号 (})。这就是您收到此错误的原因。仔细查看您的代码。你会找到解决办法的。

【讨论】:

  • 你确定他失踪了吗?
  • @GrIsHu,是的,我通过查看这段代码确定。因为在 catch 块之后,他只需要关闭一个括号,但他试图关闭两个括号。你可以看看他的代码。
  • 这就是我所说的最后一个括号是额外的,这就是为什么他需要删除它,而不是添加任何额外的括号。 @无辜杀手
【解决方案3】:
}catch (UiObjectNotFoundException e){}
}

改成

}catch (UiObjectNotFoundException e){
}

请注意,您在接球后立即关闭了左括号

【讨论】:

  • 还是出现语法错误“{”
【解决方案4】:

最后一个括号是不必要的,所以删除那个括号。

错误本身表示删除最后一个括号}

  } Error message appears here : Syntax error on token "}", delete this token

如果您收到错误"break cannot be used outside of a loop or a switch"

然后改变你的 if 条件如下:

      if(!okDialog.exists())
               System.out.println("unable to find"); 
                getUiDevice().pressHome();

【讨论】:

    【解决方案5】:

    试试这个:

    public void ok() throws UiObjectNotFoundException{
                //trying to find ok
                       try{
                           UiObject okDialog = new UiObject(new UiSelector().text("ok"));
                       if(okDialog.exists()){
                           System.out.println("Success");   
                           break;//if ok exists it should come out
                       }
                       //if ok not exists
                       if(!okDialog.exists()){
                             System.out.println("retrying");
                             getUiDevice().pressHome();
                             UiObject enterButton = new UiObject(new UiSelector().text("enter"));
                             enterButton.clickAndWaitForNewWindow();  
                       }
                       //validating again to check ok exists
                       if(okDialog.exists()){
                             //break;
                       }
                       else  {
                             //if ok not exists again printing the belwo message
                             System.out.println("unable to find"); 
                             getUiDevice().pressHome();
                       }
        //Here if ok not exists after going to home, i want to stop the whole script here..how to do this?
                       }catch (UiObjectNotFoundException e) {}    
    
        } 
    

    【讨论】:

    • 我试过了..“break cannot be used outside of a loop or a switch”错误出现在代码中任何“break”的地方
    • 请参阅我在回答中评论了这个中断。在 if 条件下不能使用 Break 这就是您收到此错误的原因。 @user3089474
    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多