正如标题所示,我一直在努力解决这个错误,这个错误是在我成功实现了一个允许用户更改电子邮件和姓名的功能之后突然出现的。请注意,我目前正在使用firebase身份验证和他们的云firestore。错误发生在我再次尝试启动设备之后。你的意图有什么问题吗?我对android开发还比较陌生,所以我在某些方面的知识非常有限。唯一指导我的资源是youtube上的教程。
我将分享我的密码,我真的很感激如果你聪明的人可以帮助一个小伙子。
主活动.java
package com.example.loginregisterfire;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationMenu;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.auth.User;
public class MainActivity extends AppCompatActivity {
FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fAuth = FirebaseAuth.getInstance();
BottomNavigationView bottomView = findViewById(R.id.bottom_view);
bottomView.setOnNavigationItemSelectedListener(onNav);
getSupportFragmentManager().beginTransaction().replace(R.id.container,
new UserProfile()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener onNav = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selected = null;
switch (item.getItemId()){
case R.id.user_profile_tab:
selected = new UserProfile();
break;
case R.id.calendar_tab:
selected = new Calendar();
break;
case R.id.donate_blood_tab:
selected = new Donate_Blood();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container,
selected).commit();
return true;
}
};
@Override
protected void onStart() {
super.onStart();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
Intent intent = new Intent(MainActivity.this, Login.class);
startActivity(intent);
finish();
}
}
}
用户配置文件.java
package com.example.loginregisterfire;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationMenu;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.storage.StorageReference;
import javax.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class UserProfile extends Fragment {
TextView usernameText, fullNameText, email;
FirebaseFirestore fStore;
FirebaseAuth fAuth;
ImageView profilePicture;
Button editProfile_btn,logoutBtn;
StorageReference storageReference;
@androidx.annotation.Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @androidx.annotation.Nullable ViewGroup container, @androidx.annotation.Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_profile,container,false);
return view;
}
public UserProfile() {
}
@Override
public void onActivityCreated(@androidx.annotation.Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
usernameText = getActivity().findViewById(R.id.usernameText);
fullNameText = getActivity().findViewById(R.id.fullNameText);
email = getActivity().findViewById(R.id.emailText);
profilePicture = getActivity().findViewById(R.id.profilePicture);
logoutBtn = getActivity().findViewById(R.id.logout_btn);
editProfile_btn = getActivity().findViewById(R.id.editProfile_btn);
}
@Override
public void onStart() {
super.onStart();
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
String userID;
userID = fAuth.getCurrentUser().getUid();
final DocumentReference documentReference = fStore.collection("users").document(userID);
documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
//usernameText.setText(documentSnapshot.getString("Username"));
fullNameText.setText(documentSnapshot.getString("FullName"));
email.setText(documentSnapshot.getString("Email"));
if(documentSnapshot.get("isAdmin") != null){
usernameText.setText("Admin");
}
if(documentSnapshot.get("isDonor") != null){
usernameText.setText("Donor");
}
}
});
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getActivity().getApplicationContext(), Login.class));
getActivity().finish();
}
});
editProfile_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), EditProfile.class);
i.putExtra("FullName", fullNameText.getText().toString());
i.putExtra("Email",email.getText().toString());
startActivity(i);
}
});
}
public void logout(View view){
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getActivity().getApplicationContext(), Login.class));
getActivity().finish();
}
}
// startActivity(new Intent(getActivity().getApplicationContext(), Login.class));
编辑配置文件.java
package com.example.loginregisterfire;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.HashMap;
import java.util.Map;
public class EditProfile extends AppCompatActivity {
public static final String TAG = "TAG";
EditText profileFull, profileEmail;
ImageView profileImageView;
Button saveProfile_btn;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
FirebaseUser user;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
Intent data = getIntent();
String fullName = data.getStringExtra("FullName");
String email = data.getStringExtra("Email");
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
user = fAuth.getCurrentUser();
storageReference = FirebaseStorage.getInstance().getReference();
profileFull = findViewById(R.id.profileFullName);
profileEmail = findViewById(R.id.profileEmailAddress);
profileImageView = findViewById(R.id.profileImageView);
saveProfile_btn = findViewById(R.id.saveProfile_btn);
profileImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(EditProfile.this, "Profile Image Clicked", Toast.LENGTH_SHORT).show();
}
});
saveProfile_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(profileFull.getText().toString().isEmpty() || profileEmail.getText().toString().isEmpty()){
Toast.makeText(EditProfile.this, "Field(s) can't be left empty", Toast.LENGTH_SHORT).show();
return;
}
final String email = profileEmail.getText().toString();
user.updateEmail(email).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
DocumentReference docRef = fStore.collection("users").document(user.getUid());
Map<String,Object> edited = new HashMap<>();
edited.put("Email", email);
edited.put("FullName", profileFull.getText().toString());
docRef.update(edited).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(EditProfile.this, "Profile Updated", Toast.LENGTH_SHORT).show();
//startActivity(new Intent(getApplicationContext(),MainActivity.class));
Intent intent = new Intent(EditProfile.this, MainActivity.class);
startActivity(intent);
finish();
}
});
Toast.makeText(EditProfile.this, "Email is changed.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(EditProfile.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
profileFull.setText(fullName);
profileEmail.setText(email);
Log.d(TAG, "onCreate: " + fullName + " " + email);
}
}
至于我的登录和注册
登录.java
package com.example.loginregisterfire;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Login extends AppCompatActivity {
TextInputEditText textInputEditTextEmail, textInputEditTextPassword;
Button buttonLogin;
TextView textviewSignup;
ProgressBar progressBar;
FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
textInputEditTextEmail = findViewById(R.id.email);
textInputEditTextPassword = findViewById(R.id.password);
buttonLogin= findViewById(R.id.btnLogin);
progressBar = findViewById(R.id.progress);
textviewSignup = findViewById(R.id.signUpText);
fAuth = FirebaseAuth.getInstance();
// if user is already logged in, no need to re-enter credential the second time.
if(fAuth.getCurrentUser() != null){
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = textInputEditTextEmail.getText().toString().trim();
String password = textInputEditTextPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
textInputEditTextPassword.setError("Email is Required.");
}
if(TextUtils.isEmpty(password)){
textInputEditTextPassword.setError("Password is Required.");
}
if(password.length () < 6){
textInputEditTextPassword.setError("Password Must be More Than 6 Characters");
}
progressBar.setVisibility(View.VISIBLE);
//authenticate the user
fAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(Login.this, "Logged In Successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}else{
Toast.makeText(Login.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
textviewSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), SignUp.class));
}
});
}
}
注册
package com.example.loginregisterfire;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class SignUp extends AppCompatActivity {
public static final String TAG = "TAG";
TextInputEditText textInputEditTextFullName, textInputEditTextUsername, textInputEditTextPassword, textInputEditTextEmail;
Button buttonSignUp;
CheckBox isAdmin, isDonor;
TextView textViewLogin;
ProgressBar progressBar;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
isAdmin = findViewById(R.id.admincheckBox);
isDonor = findViewById(R.id.donorcheckBox);
textInputEditTextFullName =findViewById(R.id.fullname);
textInputEditTextUsername = findViewById(R.id.username);
textInputEditTextEmail = findViewById(R.id.email);
textInputEditTextPassword = findViewById(R.id.password);
buttonSignUp = findViewById(R.id.buttonSignUp);
textViewLogin = findViewById(R.id.loginText);
progressBar = findViewById(R.id.progress);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
// test error
// if user is already logged in, no need to re-enter credential the second time.
//if(fAuth.getCurrentUser() != null){
// finish();
// startActivity(new Intent(getApplicationContext(), MainActivity.class));
// }
isAdmin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked()){
isDonor.setChecked(false);
}
}
});
isDonor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked()){
isAdmin.setChecked(false);
}
}
});
buttonSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = textInputEditTextEmail.getText().toString().trim();
String password = textInputEditTextPassword.getText().toString().trim();
final String fullName = textInputEditTextFullName.getText().toString();
final String Username = textInputEditTextUsername.getText().toString();
if(TextUtils.isEmpty(email)){
textInputEditTextEmail.setError("Email is Required.");
}
if(TextUtils.isEmpty(password)){
textInputEditTextPassword.setError("Password is Required.");
}
if(password.length () < 6){
textInputEditTextPassword.setError("Password Must be More Than 6 Characters");
}
//checkbox validation
if(!(isAdmin.isChecked() || isDonor.isChecked())){
Toast.makeText(SignUp.this, "Select The Account Type", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
fAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(SignUp.this, "User Created", Toast.LENGTH_SHORT).show();
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String, Object> user = new HashMap<>();
user.put("FullName", fullName);
user.put("Email", email);
user.put("Username", Username);
if(isAdmin.isChecked()){
user.put("isAdmin", "1");
}
if(isDonor.isChecked()){
user.put("isDonor", "1");
}
documentReference.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "onSuccess: user Profile is created for "+userID);
}
});
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}else {
Toast.makeText(SignUp.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
textViewLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), Login.class));
}
});
}
}
grad尔
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.example.loginregisterfire"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-firestore:21.4.2'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'com.google.android.material:material:1.3.0-alpha02'
implementation 'com.android.support:multidex:1.0.1'
}
很抱歉邮件太长。我真的很抱歉。但我现在很迷茫。我已经搜索了所有的解决方案,但没有发现任何有用的(至少在我的情况下)。
我真的很感激一个善良的灵魂能帮我解决这个问题。谢谢您。
暂无答案!
目前还没有任何答案,快来回答吧!