2016-12-19 36 views
1

我制成,其中我用机器人源码数据库来存储的登录信息即用户名和密码一个简单的登录活动。当用户在注册活动中点击创建账户按钮时,他/她将被转移到另一个活动,在那里他们可以看到他们的用户名和通行证。在该屏幕中,我需要检查首选项是否清晰,然后根据决定更改按钮的文本。不能够清除共享偏好

我通过活动之间的信息使用SharedPreference,但我不能删除或清除它,当用户点击登出..以下是这两种活动的代码..

这里是我的SignUp.java

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.signup); 

    // get Instance of Database Adapter 
    loginDataBaseAdapter=new LoginDataBaseAdapter(this); 
    loginDataBaseAdapter=loginDataBaseAdapter.open(); 

    // Get Refferences of Views 
    editTextUserName=(EditText)findViewById(R.id.editTextUserName); 
    editTextPassword=(EditText)findViewById(R.id.editTextPassword); 
    editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword); 
    tv=(TextView)findViewById(R.id.textView); 

    btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount); 
    btnCreateAccount.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String userName=editTextUserName.getText().toString(); 
      String password=editTextPassword.getText().toString(); 
      String confirmPassword=editTextConfirmPassword.getText().toString(); 

      // check if any of the fields are vaccant 
      if(userName.equals("")||password.equals("")||confirmPassword.equals("")) { 
       Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show(); 
       return; 
      } 
      // check if both password matches 
      if(!password.equals(confirmPassword)) { 
       Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show(); 
       return; 
      } 
      if(password.length()<8) { 
       Toast.makeText(getApplicationContext(),"Password must be 8 digits longer",Toast.LENGTH_LONG).show(); 
      } 
      else { 
       // Save the Data in Database 
       loginDataBaseAdapter.insertEntry(userName, password); 
       try { 

        sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 

        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putString(Name,userName); 
        editor.putString(Pass,password); 
        editor.commit(); 

       } catch (NullPointerException e) { 
        e.printStackTrace(); 
       } 

       intent=new Intent(getApplicationContext(),AfterLogin.class); 
       startActivity(intent); 
       Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    loginDataBaseAdapter.close(); 
} 

而且AfterLogin.Java

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.afterlogin); 

    username=(TextView)findViewById(R.id.Username); 
    name=(TextView)findViewById(R.id.Name); 
    user=(TextView)findViewById(R.id.User); 
    pass=(TextView)findViewById(R.id.Pass); 
    sout=(Button)findViewById(R.id.SignOut); 

    s= sharedPreferences.getString(Name,""); 
    us=sharedPreferences.getString(Pass,""); 

    username.setText(s); 
    name.setText(s); 
    user.setText(s); 
    pass.setText(us); 

    sout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      SharedPreferences.Editor editor=sharedPreferences.edit(); 
      editor.clear(); 
      editor.commit(); 

      String n=sharedPreferences.getString(Name,""); 

      if (n == null) { 
       sout.setText("Signed Out"); 
      } 
     } 
    }); 
} 
+0

代码看起来不错,我猜。尝试拨打[MCVE] –

+0

凡AfterLogin初始化'sharedPreferences'对象? –

回答

1

AfterLogin.java你检查

n ==可空

,但传递的默认值是为空字符串N

字符串n = sharedPreferences.getString(名称, “”); //空

变化

if (n==null) { 
    sout.setText("Signed Out"); 
} 

if (n.isEmpty()) { 
    sout.setText("Signed Out"); 
} 
+0

谢谢..兄弟你节省了我很多时间... –

0

你能否通过提交明确的尝试,并在一行提交。像editor.clear()。commit()或editor.clear()。apply()。这可能对你有所帮助,因为你在修改之后对编辑器对象提交或应用了更改。

0

这个问题可能是您正在使用String n=sharedPreferences.getString(Name,"");它总是返回的东西。如果首选项不存在,则为缺省值;如果不存在,则为缺省值。

要检查是否偏好已被删除,我最好使用String n=sharedPreferences.contains(Name);返回boolean,显示如果偏好存在与否。

希望它有帮助!