尝试“Finally”语句时出现语法错误

时间:2012-03-03 11:45:31

标签: java android syntax syntax-error

我已经输入了“最终”声明,但是我收到了语法错误,我对如何纠正它们一无所知,我已经尝试了所有内容,但无济于事。我的类得到两个编辑文本字段中的字符串,如果选中该复选框,则将两个字符串保存到文件中,以便稍后调用,然后,如果编辑文本中没有任何内容,则显示一个吐司。如果这是他们的第一次,他们的数据(用户并传递它保存),如果他们在通过检查文件之前完成此操作,它将通过意图转到另一个类。哦,对不起我的错误代码,我是一个新的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

3 个答案:

答案 0 :(得分:6)

您没有关闭第二个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()) {

答案 1 :(得分:2)

你忘记了结束括号。

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

应该是:

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

由于您没有发布整个Java文件,我无法验证整个代码。您可以尝试在标记^的代码段末尾添加另一个结束括号。

答案 2 :(得分:0)

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

相关问题