令牌“if”上的语法错误,AnnotationName无效

时间:2011-07-11 19:05:12

标签: java android

您好我正在使用android而我正在尝试在此类周围添加“if”语句,但是如果“,无效的AnnotationName”,我会收到错误“令牌上的语法错误” 有什么帮助吗?

    private String str = "0"; 
if(str == 0){
     public boolean onKeyDown(int keyCode, KeyEvent event)
     {

         //Handle the back button
         if(keyCode == KeyEvent.KEYCODE_BACK)
         {
             //Ask the user if they want to quit
             new AlertDialog.Builder(this)
             .setIcon(android.R.drawable.ic_dialog_alert)
             .setTitle("Coupon")
             .setMessage("Do you want a coupon texted to you?")
             .setPositiveButton("YES!", new DialogInterface.OnClickListener()
             {
                 @Override
                 public void onClick(DialogInterface dialog, int which)
                 {
                     //Stop the activity
                     Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show(); 
                     finish();
                 }
             })
             .setNegativeButton("Not now", new DialogInterface.OnClickListener()
             {
                 @Override
                 public void onClick(DialogInterface dialog, int which) 
                 {
                     //Stop the activity
                     finish();
                 }
             })
             .show();        
             return true;
         }
         else
         {
             return super.onKeyDown(keyCode, event);
         }

     }
}
else
{}

2 个答案:

答案 0 :(得分:13)

您不能将控制结构(如if语句)放在函数之外。

答案 1 :(得分:1)

您必须使用equals字符串方法:

if (str.equals("0")){
    ...
}