为了账号安全,请及时绑定邮箱和手机立即绑定

我的共享偏好没有正确共享

我的共享偏好没有正确共享

隔江千里 2023-06-08 17:11:09
我有一个应用程序,你可以将图像上传到我公司的服务器所以用户输入他们的登录详细信息、电子邮件、密码和 clientID(4 位代码)(在 LoginActivity.java 中),然后必须将此信息传递给所有其他活动,这传递的信息然后用于构建 URL。现在我遇到的问题是 Sharedprefrences 没有正确共享......它们要么在 url 上显示为 NULL,要么只是“电子邮件”或“密码”信息在登录活动中正确保存但是当我尝试通过它失败的其他活动在这里登录活动我保存首选项public class LoginActivity extends AppCompatActivity implements TextWatcher {    SharedPreferences MyPrefs;    Intent intent;    SharedPreferences.Editor editor;    public static final String PREF_NAME= "MYPREFS";    public static final  String ID = "ClientID" ;    public static final  String EMAIL = "username" ;    public static final  String PASS = "password";    EditText email, password, id;    @SuppressLint("CommitPrefEdits")    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        Button buttonOne=findViewById(R.id.button);        buttonOne.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                Intent activity2Intent=new Intent(getApplicationContext(), MainActivity.class);                startActivity(activity2Intent);    }});        MyPrefs= getSharedPreferences(PREF_NAME, 0);        editor = MyPrefs.edit();        email=findViewById(R.id.emailtext);        password=findViewById(R.id.pwdtext);        id=findViewById(R.id.clientid);        email.setText(MyPrefs.getString(EMAIL,"username"));        password.setText(MyPrefs.getString(PASS,"password"));        id.setText(MyPrefs.getString(ID, "id"));        email.addTextChangedListener(this);        password.addTextChangedListener(this);        id.addTextChangedListener(this);        MyPrefs =getSharedPreferences(EMAIL,0);        MyPrefs =getSharedPreferences(ID,0);        MyPrefs =getSharedPreferences(PASS,0);        intent = new Intent(LoginActivity.this,CameraActivity.class);    }
查看完整描述

2 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

要将数据存储在共享首选项中,请执行以下操作:


private SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();

                    editor.putString("email", email);

                    editor.putString("ID", id);

                    editor.putString("Pass", password);

                    editor.apply();

所以我会给你一些解释,当你写 editor.putString("email", email); 它告诉编辑器将您的电子邮件放在关键的“电子邮件”上。


现在,如果您想读回这些值,请这样做:


String email = getSharedPreferences(PREF_NAME, MODE_PRIVATE).getString("email", "");

String ID= getSharedPreferences(PREF_NAME, MODE_PRIVATE).getString("ID", "");

String password= getSharedPreferences(PREF_NAME, MODE_PRIVATE).getString("Pass", "");

如果你什么都不明白,我会知道的。


查看完整回答
反对 回复 2023-06-08
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

getSharedPrerencences(String name, int mode)返回对共享首选项文件的引用name。也就是说,在行之后

MyPrefs =getSharedPreferences(EMAIL,0);
MyPrefs =getSharedPreferences(ID,0);
MyPrefs =getSharedPreferences(PASS,0);

您的变量 MyPrefs 指向名为 的共享首选项文件password,这可能不是您想要的,因为稍后您从名为MYPREFS.

此外,如果您只是从首选项中读取,则无需调用editor = MyPrefs.edit();,就像您在onCreate. 这就是为什么您会收到已禁止使用的警告@SuppressLint("CommitPrefEdits")


查看完整回答
反对 回复 2023-06-08
  • 2 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信