亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的共享偏好沒有正確共享

我的共享偏好沒有正確共享

隔江千里 2023-06-08 17:11:09
我有一個應用程序,你可以將圖像上傳到我公司的服務器所以用戶輸入他們的登錄詳細信息、電子郵件、密碼和 clientID(4 位代碼)(在 LoginActivity.java 中),然后必須將此信息傳遞給所有其他活動,這傳遞的信息然后用于構建 URL?,F在我遇到的問題是 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 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號