![](/img/trans.png)
[英]Saving an Object in SharedPreferences and Reading it From an Activity in Android Studio Using GSON
[英]Error Using SharedPreferences() Method In Android Studio (Login Activity) After Adding Firebase Authentication
我的代碼一直有效,直到我將 Firebase 添加到它。 這是我的登錄頁面,SharedPreferences 不起作用。 當我點擊登錄按鈕時,密碼作為用戶名和密碼彈出 - 然后當我重新運行模擬器時 - 用戶名和密碼為 null。 請幫我找出錯誤:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class Login extends AppCompatActivity {
EditText username;
EditText etPassword;
Button loginBtn;
Button registerBtn;
private SharedPreferences mPreferences;
private SharedPreferences.Editor mEditor;
private static String TAG = "Michael's Tracking --> ";
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d(TAG, "Start onCreate");
username = findViewById(R.id.username);
etPassword = findViewById(R.id.etPassword);
loginBtn = findViewById(R.id.registerBtn);
registerBtn = findViewById(R.id.toLoginBtn);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mEditor = mPreferences.edit();
mAuth = FirebaseAuth.getInstance();
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
currentUser.reload();
}
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = username.getText().toString();
String pass = etPassword.getText().toString();
String stName = email;
mEditor.putString(getString(R.string.name), stName);
mEditor.commit();
String stPassword = pass;
mEditor.putString(getString(R.string.password), stPassword);
mEditor.commit();
checkSharedPreferences();
mAuth.signInWithEmailAndPassword(email, pass)
.addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
startActivity(new Intent(Login.this, MainPage.class));
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerPage = new Intent(Login.this, Register.class);
startActivity(registerPage);
}
});
}
private void checkSharedPreferences() {
String stName, stPassword;
stName = mPreferences.getString(getString(R.string.name), "");
stPassword = mPreferences.getString(getString(R.string.password), "");
username.setText(stName);
etPassword.setText(stPassword);
}
}```
Thank you very much!
checkSharedPreferences()
方法不應在onClick
內部調用,它用於恢復數據,因此應在開始時調用它,因此在onCreate
引用View
s( findViewById
)和首選項( mPreferences
, mEditor
)之后
成功登錄后使用共享首選項存儲用戶名和密碼。
mAuth.signInWithEmailAndPassword(email, pass)
.addOnCompleteListener(Login.this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
mEditor.putString("Useranme", stName);
mEditor.putString("Password", stPassword);
mEditor.commit();
startActivity(new Intent(Login.this, MainPage.class));
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.