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

Android集成Firebase认证并添加用户头像上传功能时出现构造函数参数匹配错误

Hey there, let's fix those constructor mismatch errors you're seeing with your User model and Firebase integration! Here's a step-by-step breakdown of the problem and solutions:

What's Causing the Errors?

Your User class currently only has one constructor that requires 6 parameters (name, email, contact, city, age, pimage), but:

  1. In your register_doctor() method, you're trying to create a User object with only 5 parameters (missing pimage).
  2. In your upload_image() method, you're trying to create a User object with just 1 parameter (only the image URI).

Additionally, your upload method has a path mismatch with your registration code, which would lead to updating the wrong database node.

Solution 1: Add Overloaded Constructors to User Class

First, update your User model to include multiple constructors for different use cases (keep the empty constructor—Firebase requires it for deserialization):

public class User {
    public String name, email, password, contact, city, age, pimage;
    
    // Required empty constructor for Firebase
    public User(){ }
    
    // Constructor for registration (without pimage, set default value)
    public User(String name, String email, String contact, String city, String age){
        this.name = name;
        this.email = email;
        this.contact = contact;
        this.city = city;
        this.age = age;
        this.pimage = ""; // Set empty default or a default avatar URL
    }
    
    // Full constructor for when you have all fields
    public User(String name, String email, String contact, String city, String age, String pimage){
        this.name = name;
        this.email = email;
        this.contact = contact;
        this.city = city;
        this.age = age;
        this.pimage = pimage;
    }
}

Solution 2: Fix the Registration Method

Now your register_doctor() code will work as-is, since the 5-parameter constructor matches your existing call:

// This line now uses the correct overloaded constructor
User user = new User(dname, demail, dcontact, dcity, dage);

Solution 3: Fix the Avatar Upload Method (Critical!)

Don't create a new User object to update just the avatar—this will overwrite all existing user data with null values for missing fields. Instead, update only the pimage field in the existing user node, and make sure your database path matches the one used during registration:

private void upload_image() {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference uploader = storage.getReference("Image1"+ new Random().nextInt(50));
    
    uploader.putFile(filepath)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    uploader.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            FirebaseDatabase db = FirebaseDatabase.getInstance();
                            // Match the exact path from registration: doctors/Doctors_Registration/[email-with-dots-replaced]
                            String userEmailKey = et_email.getText().toString().trim().replace(".", ",");
                            DatabaseReference userRef = db.getReference("doctors")
                                    .child("Doctors_Registration")
                                    .child(userEmailKey);
                            
                            // Update ONLY the pimage field
                            userRef.child("pimage").setValue(uri.toString())
                                    .addOnSuccessListener(unused -> 
                                        Toast.makeText(signup.this, "Profile Image Uploaded", Toast.LENGTH_SHORT).show()
                                    )
                                    .addOnFailureListener(e -> 
                                        Toast.makeText(signup.this, "Failed to update avatar: " + e.getMessage(), Toast.LENGTH_SHORT).show()
                                    );
                        }
                    });
                }
            })
            .addOnFailureListener(e -> 
                Toast.makeText(signup.this, "Upload failed: " + e.getMessage(), Toast.LENGTH_SHORT).show()
            );
}

Key Notes to Remember

  • Always keep the empty constructor in your User class—Firebase needs it to read data from the database.
  • Match database paths across registration and update operations (don't forget the doctors parent node and replacing dots in emails, since Firebase doesn't allow dots in keys).
  • When updating partial user data, use child("fieldName").setValue() instead of overwriting the entire object to avoid losing existing data.

内容的提问来源于stack exchange,提问作者Jawad Khan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 13:52:31