简体   繁体   English

SharedPreferences不保存我的用户名

[英]SharedPreferences doesn't save my username

The SharedPreferences doesn't save my username while I don't get any error. 我没有收到任何错误时,SharedPreferences不会保存我的用户名。 I have checked my method with some tutorials and I can't find what's wrong. 我已经通过一些教程检查了我的方法,但找不到错误所在。 I have tried both commit() and 'apply()' despite that can't make here a difference. 我已经尝试了commit()和'apply()',尽管那没有什么不同。

Someone who finds the error? 有人发现错误?

my code: 我的代码:

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");

}

And the second class: 第二类:

    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);
}

Btw I know that Postdata is outdated. 顺便说一句,我知道Postdata已经过时了。 I am looking to replace it. 我正在寻找替换它。

Issue : currently your taking the value of username in onCreate when you don't have the user input so 问题:当前,当您没有用户输入时,您在onCreate中使用username的值,因此

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

should be executed after user input is available like in onclick 应该在用户输入可用后(如onclick

or 要么

@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();

Note : name was not previously used to store any value , i guess wanted to use name2 注意: name以前没有用来存储任何值,我name2使用name2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM