SharedPreferences不保存我的用户名

时间:2017-08-23 15:57:29

标签: java android sharedpreferences

当我没有收到任何错误时,SharedPreferences不会保存我的用户名。我用一些教程检查了我的方法,但我找不到有什么问题。我已经尝试了commit()和'apply()',尽管这不能解决这个问题。

发现错误的人?

我的代码:

public class MainActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener {

EditText etUsername, etPassword;
ImageButton btnLogin;
String username;

CheckBox ckbx_login;
public static final String PREFS_NAME = "MyPrefsFile";





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);






    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    btnLogin = (ImageButton) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(this);
    username = etUsername.getText().toString();




}



@Override
public void processFinish(String result) {
    if(result.equals("success")) {

        SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(PREFS_NAME,0).edit();

        editor.putString("loginname",username);
        editor.commit();



        Intent in = new Intent(this, SubActivity.class);
        String naam2= etUsername.getText().toString();
        in.putExtra("naam2",naam2);
        startActivity(in);

    }
    else{
        Toast.makeText(this,"Inloggen mislukt", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onClick(View view) {
    HashMap postData = new HashMap();
    postData.put("mobile", "android");
    postData.put("txtUsername", etUsername.getText().toString());
    postData.put("txtPassword", etPassword.getText().toString());

    PostResponseAsyncTask task = new PostResponseAsyncTask(this, postData);
    task.execute("http://exemple.com");

}

第二节课:

    public static final String PREFS_NAME = "MyPrefsFile";
String name;


TextView msg_welcome;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_sub);

    msg_welcome = (TextView) findViewById(R.id.txtVwelcome);

    SharedPreferences prefs = this.getSharedPreferences(PREFS_NAME,0);
    String restored_loginname = prefs.getString("loginname",null);
    if(restored_loginname != null) {
        name = prefs.getString("name", "No name defined");



}
    Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
}
public void scan(View View){
    zXingScannerView = new ZXingScannerView(getApplicationContext());
    setContentView(zXingScannerView);
    zXingScannerView.setResultHandler(this);
    zXingScannerView.startCamera();


}

@Override
protected void onPause() {
    super.onPause();
    zXingScannerView.stopCamera();
}

@Override
public void handleResult(Result result) {
  //  Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_LONG).show();
    Intent i = new Intent(getApplicationContext(), Avt_form2.class);
    String qrcode = result.getText();
    i.putExtra("qrcode",qrcode);
    startActivity(i);
}

顺便说一下,我知道Postdata已经过时了。我想替换它。

1 个答案:

答案 0 :(得分:2)

问题:当您没有用户输入时,目前您在username中获取onCreate的值

username = etUsername.getText().toString(); 

应该在用户输入可用后执行,如onclick

@Override
public void processFinish(String result) {
    if(result.equals("success")) {

        SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(PREFS_NAME,0).edit();
        username = etUsername.getText().toString(); 
      //^^^^^^^^^
        editor.putString("loginname",username);
        editor.commit();

注意:name之前未用于存储任何值,我想使用name2