0

我正在尝试使用 SharedPreferences 保存登录凭据的代码。我能够或相信凭据已成功保存,但是在尝试通过将编辑中的凭据与保存的凭据进行比较来登录时。我不断收到“密码错误”错误。不知道我在忽略什么。登录代码如下。

登录:

   public class AccessApp extends Activity implements OnClickListener {
private SharedPreferences sp;
String user,pass;
Button lBttn,cBttn;
EditText uname,pword;
Intent i;

int flag=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lBttn=(Button)findViewById(R.id.login_button);
    cBttn=(Button)findViewById(R.id.cancel_button);
    uname=(EditText)findViewById(R.id.username);
    pword=(EditText)findViewById(R.id.password);

    lBttn.setOnClickListener(this);
    cBttn.setOnClickListener(this);
}
public void onClick(View arg0) {

    sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE);
    user=sp.getString("USERNAME", "");
    pass=sp.getString("PASSWORD","");


    if(lBttn==arg0){
            if((uname.getText().toString().compareTo(user)==0)&& 
               (pword.getText().toString().compareTo(pass)==0))

            {
          Toast.makeText(this, "You are Logged In", 20000).show();

               Intent intent;
               intent=new Intent(this,details.class);
               startActivity(intent);
              flag=1;
            }

        else
           {
            Toast.makeText(this, "Wrong Username or Password",20000).show();
            flag=0;   
           }       
        } 

登记:

     public class SharedPrefLoginActivity extends Activity implements OnClickListener {

private SharedPreferences sp;

Intent i;
Button regBttn,rtnBttn;
EditText rName,rPwd;
String user,pass,cpass,chk;
String stat="a";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{


    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);


    rName=(EditText)findViewById(R.id.reg_uname);
    rPwd=(EditText)findViewById(R.id.reg_pswd);
    regBttn=(Button)findViewById(R.id.reg_button);
    rtnBttn=(Button)findViewById(R.id.rtn_button); 

    regBttn.setOnClickListener(this);
    rtnBttn.setOnClickListener(this);
    sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE);
    chk=sp.getString("USERNAME", "");
    if(chk.length()!=0){
        sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE); 

        i=new Intent(this,AccessApp.class);
        startActivity(i);      
    }

}

public void onClick(View arg0) {
    user=rName.getText().toString();
    pass=rPwd.getText().toString();
    if(arg0==regBttn){

        if((user.length()!=0))
        {
            if((pass.length()!=0))
            {

        sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
        Editor myEditor=sp.edit();
        myEditor.putString("USERNAME", user);
        myEditor.putString("PASSWORD", pass);
        myEditor.commit();
        Toast.makeText(this, "Registration is successfull",10000).show();
        i=new Intent(this,AccessApp.class);
        startActivity(i);
        }
        else
         {
          Toast.makeText(this, "Please Enter password", 10000).show();  
         }
         }
        else{
            Toast.makeText(this,"Please Enter Username",10000).show();
         }
      }
4

2 回答 2

1

您不能使用 == 运算符比较 Button 对象。相反,请尝试使用:

if(arg0.getId()==R.id.login_button){
于 2012-09-20T17:31:04.267 回答
0

导致问题的问题如下

登录:

    sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE); /* change "Register" to "AccessApp" */

登记:

    sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
于 2012-09-26T22:02:16.017 回答