gpt4 book ai didi

java - SharedPreferences 不保存我的用户名

转载 作者:行者123 更新时间:2023-12-02 02:38:40 25 4
gpt4 key购买 nike

当我没有收到任何错误时,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 已经过时了。我正在寻找替换它。

最佳答案

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

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

关于java - SharedPreferences 不保存我的用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45844440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com