java - token "if"上的语法错误,无效的 AnnotationName

标签 java android

您好,我正在使用 android,我正尝试在此类周围放置一个“if”语句,但我收到错误“ token “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
{}

最佳答案

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

关于java - token "if"上的语法错误,无效的 AnnotationName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6655113/

相关文章:

java - 使用Texturepacker/LibGDX 制作动画 - 渲染错误

android - 无法使用Android手机调试连接到本地主机

android - Gradle:如何在控制台中显示 androidTest 结果?

java - 在 ListView android中定位字符串

android - 谷歌驱动器修改日期给出错误的时间

java - 致命异常:main (after java to android project conversion)

java - jOOQ "IN"具有 N 次元组的谓词

java - 关于 LL1 非递归解析器中的 AST 构造

java - 无法在通过 GWT-RPC 传递到服务器的 ArrayList 中找到对象

java - 如何将数学工作拆分为工作线程 Java