2014-11-05 7 views
0

나는 사용자 자격 증명이 된 SharedPreferences를 사용하여, 나는이 방법으로 그 일을하고 저장하려고하지만 당신은 null 값-활동이 처음 만들어, user_name를 저장하는저장 자격 증명

public class MainActivity extends Activity { 
    public EditText UserName; 
    public EditText Password; 
    public Button mButton; 
    public String user_name; 
    public String pass_word; 
    public CheckBox mCheckSavePassword; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     UserName = (EditText) findViewById(R.id.textUserName); 
     Password = (EditText) findViewById(R.id.textPassword); 
     mButton = (Button) findViewById(R.id.buttonLogin); 
     mCheckSavePassword = (CheckBox) findViewById(R.id.checkSavePassword); 

     mCheckSavePassword = (CheckBox) findViewById(R.id.checkSavePassword); 
     SharedPreferences.Editor preferencesEditor = preferences.edit();      
     preferencesEditor.putString("prefUserName", user_name);     
     if(mCheckSavePassword.isChecked()) { 
      preferencesEditor.putString("prefPassword", pass_word); 
     }     
     preferencesEditor.putBoolean("prefSavePassword", mCheckSavePassword.isChecked()); 
     preferencesEditor.apply(); 
    } 
+1

어떻게 작동하지 않는 것을 알고 계십니까? –

+0

다시 입력 할 때 자격 증명을 입력해야하기 때문에 – Andrew

+0

사용하지 않는 코드가 표시되도록 환경 설정 값을 읽는 데 사용하는 코드를 게시하십시오. –

답변

2

작동하지 않습니다 pass_word은 초기화되지 않았습니다. 당신이 이 환경 설정의 값을 읽고 그 값으로 EditTexts를 업데이트 있어야 할 곳에

onCreate이다. 저장은 버튼을 눌렀을 때와 같은 일부 사용자 상호 작용의 결과로 수행되어야합니다.

+0

이 옵션이 있습니다 – Andrew

+0

고마워요. – Andrew