【问题标题】:Getting Syntax error when trying the "Finally" statment尝试“Final”语句时出现语法错误
【发布时间】:2012-03-21 16:45:40
【问题描述】:

我已经输入了“finally”语句,但我收到语法错误,我不知道如何纠正它们,我已经尝试了所有方法,但无济于事。我的类从两个编辑文本字段中获取字符串,如果选中该复选框,它将两个字符串保存到一个文件中,以便稍后调用,然后,如果编辑文本中没有任何内容,它会显示一个 toast。如果这是他们第一次,他们的数据(用户并保存传递),如果他们之前通过检查文件完成了此操作,则通过意图进入另一个类。哦,抱歉我的代码不好,我是一名新的 Java 程序员,我正在努力使其尽可能整洁。其次,如果有比我更好的编码方式,请随时更改,

谢谢。

用 ^ 标记的错误。

代码:

Button send;
    EditText user;
    EditText pass;
    CheckBox staySignedIn;
    FileOutputStream Fos;
    String a;
    String b;
    String string = a;
    String string2 = b;


    String FILENAME = "userandpass";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        send = (Button) findViewById(R.id.bLogIn);
        user = (EditText) findViewById(R.id.eTuser);
        pass = (EditText) findViewById(R.id.eTpassword);
        staySignedIn = (CheckBox) findViewById(R.id.Cbstay);
        send.setOnClickListener(this);

        File file = getBaseContext().getFileStreamPath(FILENAME);
        if (file.exists())
            ;
        Intent i = new Intent(LogIn.this, ChatService.class);
        startActivity(i); ^
    }


    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bLogIn:
            if (pass.length() == 0)
                Toast.makeText(this,
                        "Try to type in your username and password again!",
                        Toast.LENGTH_LONG).show();
            else if (user.length() == 0)
                Toast.makeText(this,
                        "Try to type in your username and password again!",
                        Toast.LENGTH_LONG).show();
            else {

            if (staySignedIn.isChecked()) {


                String a = user.getText().toString();
                String b = pass.getText().toString();
                File f = new File(FILENAME);
                try {
                    Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                    if (Fos != null) {
                    Fos.write(a.getBytes());
                    Fos.write(b.getBytes());
                    }
                    Fos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace(); ^
                finally  {
                        String u = user.getText().toString();
                        String p = pass.getText().toString();
                        Bundle send = new Bundle();
                        send.putString("key", u);
                        send.putString("key1", p);
                        Intent c = new Intent(LogIn.this, logincheck.class);
                        c.putExtra("key", u);
                        c.putExtra("key1", p);
                        startActivity(c);
                        Toast.makeText(this, "Were signing you in!", Toast.LENGTH_LONG)
                                .show();
                        break;
                    }




            }
        }

    } ^

错误

Syntax error, insert "}" to complete Block  LogIn.java  /Banana Phone/src/com/gta5news/bananaphone  line 53 Java Problem
Description Resource    Path    Location    Type
Syntax error, insert "}" to complete ClassBody  LogIn.java  /Banana Phone/src/com/gta5news/bananaphone  line 36 Java Problem
Description Resource    Path    Location    Type
Syntax error, insert "}" to complete MethodBody LogIn.java  /Banana Phone/src/com/gta5news/bananaphone  line 53 Java Problem
Description Resource    Path    Location    Type
Syntax error, insert "}" to complete MethodBody LogIn.java  /Banana Phone/src/com/gta5news/bananaphone  line 107    Java Problem

【问题讨论】:

    标签: java android syntax syntax-error


    【解决方案1】:

    您不能在 try 和 catch 块之间包含任何语句。您已在公共“void onClick(View v)”函数中包含“fos.close()”。

    【讨论】:

      【解决方案2】:

      你没有关闭第二个 catch 语句:

      } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
      
      }  //<=HERE ADD }
                  finally  {
      

      常见的结构是:

      try{
      }
      catch(...){  // since jdk7 you can put all your exception on one line
      }
      finally{
      }
      

      编辑:

      并替换:

      else {
      
              if (staySignedIn.isChecked()) {
      

      通过

       else if (staySignedIn.isChecked()) {
      

      【讨论】:

      • 谢谢,解决了错误,但我还有两个,我已经尝试了所有方法,但它不会消失。
      • if (file.exists()) ; Intent i = new Intent(LogIn.this, ChatService.class);有一个“;”如果 if 语句仅适用于下一条语句,则单独删除命中并使用括号。
      【解决方案3】:

      你忘记了右括号。

                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  finally  {
      

      应该是:

                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } finally  {
      

      由于您没有发布整个 Java 文件,我无法验证整个代码。您可以尝试在代码 sn-p 的末尾添加另一个右括号,其中您标记了 ^

      【讨论】:

      • 是的!修复了一个错误,我还有两个,我用^标记了它们。
      • 最后一个错误可以通过添加}来修复。我不能告诉你第一个错误有什么问题。我们需要错误消息来解决这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2012-08-03
      • 1970-01-01
      • 2022-06-15
      相关资源
      最近更新 更多