2012-08-16 87 views
0

我想密码保护自己的活动,我有一些问题。代码中有一些错误。编译错误:预计AnnotationName而不是

import com.foo.avanos.AvanosActivity; 
import com.foo.avanos.R; 

import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.content.Intent;; 

public class ChangePassword extends AvanosActivity{ 

    public void onCreate(Bundle savedInstanceState) { 
     setContentView(R.layout.settings); 
     Button submitButton = (Button) findViewById(R.id.button2); 
     submitButton.setOnClickListener(this); 

     Button ChangePasswordButton = (Button) findViewById(R.id.button2); 
     ChangePasswordButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent intent = new Intent(); 
       setResult(RESULT_OK, intent); 
       finish(); 
      } 

     Button EnterButton2 = (Button) findViewById(R.id.button2); 
     //Below, it's telling me 'Syntax error on tokens, AnnotationName expected instead' 
     EnterButton2.setOnClickListener(new View.OnClickListener() { 
      public void onClick2(View v) { 
       EditText passwordEditText = (EditText) findViewById(R.layout.password); 
       SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE); 
       String NewPassword = prefs.getString("password",""); 
       String CurrentPassword = prefs.getString("password","NewPassword"); 
       if(NewPassword.equals(CurrentPassword)){ 
        Editor edit = prefs.edit(); 
        edit.putString("password",passwordEditText.getText().toString()); 
        edit.commit(); 
       } 
       else { 
        Toast.makeText(getBaseContext(),"Passwords do not match",Toast.LENGTH_SHORT).show(); 
        return; 
       } 
       //Below, it's telling me 'Syntax error, insert ";" to complete Statement' 
      } 
     } 
    } 
} 
} 

首先,我想知道我是否正确地做了这件事。如果我不是,请指导我做正确的事。

+0

你有什么错误?什么是写在你的Logcat? – Erol 2012-08-16 23:55:23

+0

什么是'AvanosActivity'? – Squonk 2012-08-16 23:56:52

+0

^我的活动名称 – 2012-08-18 15:31:29

回答

0

结束您的setOnclickListener与);它没有结束,因此你得到的编译错误。

ChangePasswordButton.setOnClickListener{ 
    public void onClick2(View v) { 
     //code goes here 
    } 
}); 

同样是

EnterButton2.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
} 
}); 

的情况下并使用的onclick不onClick2

分配点击听众一个变量,然后做一个setOnclickListener。它会被读取。

+0

另外,您的所有按钮都有相同的ID – 2012-08-17 00:19:28

0

你在上下文的底部有第二个分号。删除它,然后再试一次。