You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase实时数据库环境下电商APP注册账户加载卡顿问题求助

Hey there, let's break down why your account creation is getting stuck and fix it step by step!

Core Issue Breakdown

Your loading bar stays stuck because you're not handling database operation failures properly, and there's a high chance your Firebase Realtime Database rules are blocking the write operation. Let's tackle these one by one.


1. Fix the onCancelled Method (Immediate Fix for Stuck Loading Bar)

Right now, your onCancelled method is empty. If the database request gets cancelled (due to permission errors, network issues, or invalid requests), the loading bar will never dismiss, leaving users stuck. Update this method to handle failures:

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
    loadingbar.dismiss();
    Toast.makeText(register.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_LONG).show();
}

2. Check Firebase Realtime Database Rules

By default, Firebase blocks unauthenticated reads/writes. Since your code isn't using Firebase Authentication to validate users first, the write request gets rejected silently. You have two options here:

  • Temporary Debugging: Open your Firebase Console → Realtime Database → Rules, and use these open rules (only for development—never use this in production!):
    {
      "rules": {
        ".read": true,
        ".write": true
      }
    }
    
  • Production-Grade Fix: Use Firebase Authentication to create user accounts first, then write user data. Storing plaintext passwords (like your current code does) is a major security risk—Firebase Auth handles credential storage safely for you.

Here's a revised version of your code that uses Firebase Auth for secure account creation, then writes user data to the database:

public class register extends AppCompatActivity { 
    EditText username, pass, phonenumber ; 
    Button createaccount; 
    ProgressDialog loadingbar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_register); 
        username = findViewById(R.id.username); 
        pass = findViewById(R.id.reg_password); 
        phonenumber = findViewById(R.id.reg_phone_number); 
        createaccount=findViewById(R.id.create_account); 
        loadingbar=new ProgressDialog(this); 
        createaccount.setOnClickListener(v -> CreateAccount()); 
    } 
    private void CreateAccount() { 
        String name = username.getText().toString(); 
        String phone = phonenumber.getText().toString(); 
        String password = pass.getText().toString(); 

        if(TextUtils.isEmpty(name)) { 
            Toast.makeText(register.this, "Please enter name...", Toast.LENGTH_SHORT).show(); 
        } else if (TextUtils.isEmpty(phone)) { 
            Toast.makeText(register.this, "Please enter phone...", Toast.LENGTH_SHORT).show(); 
        } else if (TextUtils.isEmpty(password)) { 
            Toast.makeText(register.this, "Please enter password...", Toast.LENGTH_SHORT).show(); 
        } else { 
            loadingbar.setTitle("Create Account"); 
            loadingbar.setMessage("Please wait, while we are checking the credentials.."); 
            loadingbar.setCanceledOnTouchOutside(false); 
            loadingbar.show(); 

            // Use Firebase Auth to create a secure account
            FirebaseAuth.getInstance().createUserWithEmailAndPassword(phone + "@yourdomain.com", password)
                    .addOnCompleteListener(task -> {
                        if (task.isSuccessful()) {
                            // Get authenticated user ID
                            String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
                            DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                            
                            // Store user data (no plaintext password!)
                            HashMap<String, Object> userDataMap = new HashMap<>();
                            userDataMap.put("phone", phone);
                            userDataMap.put("name", name);

                            rootRef.child("Users").child(userId).setValue(userDataMap)
                                    .addOnCompleteListener(dbTask -> {
                                        if (dbTask.isSuccessful()) {
                                            Toast.makeText(register.this, "Congratulations, Your account has been created..", Toast.LENGTH_SHORT).show();
                                            loadingbar.dismiss();
                                            startActivity(new Intent(register.this, login.class));
                                        } else {
                                            loadingbar.dismiss();
                                            Toast.makeText(register.this, "Failed to save user details: " + dbTask.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                        } else {
                            loadingbar.dismiss();
                            Toast.makeText(register.this, "Account creation failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        } 
    } 
}

4. Quick Checks Before Testing

  • Ensure you've added Firebase Auth and Realtime Database dependencies to your build.gradle (Module level):
    implementation 'com.google.firebase:firebase-auth:22.3.1'
    implementation 'com.google.firebase:firebase-database:20.3.0'
    
  • Never store plaintext passwords in your database—Firebase Auth manages user credentials securely, so you don't need to handle this yourself.

内容的提问来源于stack exchange,提问作者simran kaur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:21:56