出现令牌“}”错误时出现语法错误

时间:2013-12-21 06:20:43

标签: android eclipse syntax token

我收到此错误“令牌上的语法错误”}“,删除此令牌。”在最后一行,我不知道为什么我是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

5 个答案:

答案 0 :(得分:1)

如果你只知道英语知识,那么你可以在显示你的错误信息方面删除这个}

  

你不能在条件中添加中断,你需要返回而不是中断并删除{} in条件

} 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) {}

               }

答案 1 :(得分:0)

可能你缺少关闭括号(})代码中的某些位置。这就是你得到这个错误的原因。仔细查看您的代码。你会找到解决方案。

答案 2 :(得分:0)

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

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

  } 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();

答案 3 :(得分:0)

}catch (UiObjectNotFoundException e){}
}

更改为

}catch (UiObjectNotFoundException e){
}

注意你在捕获后立即关闭了开口支架

答案 4 :(得分:0)

试试这个:

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) {}    

    }