第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

隔江千里 2023-06-08 17:11:09
我有一個應(yīng)用程序,你可以將圖像上傳到我公司的服務(wù)器所以用戶輸入他們的登錄詳細(xì)信息、電子郵件、密碼和 clientID(4 位代碼)(在 LoginActivity.java 中),然后必須將此信息傳遞給所有其他活動,這傳遞的信息然后用于構(gòu)建 URL?,F(xiàn)在我遇到的問題是 Sharedprefrences 沒有正確共享......它們要么在 url 上顯示為 NULL,要么只是“電子郵件”或“密碼”信息在登錄活動中正確保存但是當(dāng)我嘗試通過它失敗的其他活動在這里登錄活動我保存首選項(xiàng)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貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個贊

要將數(shù)據(jù)存儲在共享首選項(xiàng)中,請執(zhí)行以下操作:


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

                    editor.putString("email", email);

                    editor.putString("ID", id);

                    editor.putString("Pass", password);

                    editor.apply();

所以我會給你一些解釋,當(dāng)你寫 editor.putString("email", email); 它告訴編輯器將您的電子郵件放在關(guān)鍵的“電子郵件”上。


現(xiàn)在,如果您想讀回這些值,請這樣做:


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

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


查看完整回答
反對 回復(fù) 2023-06-08
?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個贊

getSharedPrerencences(String name, int mode)返回對共享首選項(xiàng)文件的引用name。也就是說,在行之后

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

您的變量 MyPrefs 指向名為 的共享首選項(xiàng)文件password,這可能不是您想要的,因?yàn)樯院竽鷱拿麨?code>MYPREFS.

此外,如果您只是從首選項(xiàng)中讀取,則無需調(diào)用editor = MyPrefs.edit();,就像您在onCreate. 這就是為什么您會收到已禁止使用的警告@SuppressLint("CommitPrefEdits")


查看完整回答
反對 回復(fù) 2023-06-08
  • 2 回答
  • 0 關(guān)注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號