Java Firebase“;用户缺少没有参数的构造函数";
我正在尝试从Firebase中的/user/树中检索我的用户详细信息。我有以下用户对象:Java Firebase“;用户缺少没有参数的构造函数";,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在尝试从Firebase中的/user/树中检索我的用户详细信息。我有以下用户对象: public class User { private String name; private String email; private FirebaseUser firebaseUser; private String lastOnline; private LatLng latLng; private ArrayList<SwapLocation
public class User {
private String name;
private String email;
private FirebaseUser firebaseUser;
private String lastOnline;
private LatLng latLng;
private ArrayList<SwapLocation> locations;
public User(){ }
public String getName(){
if(firebaseUser.getDisplayName() != null && !firebaseUser.getDisplayName().equals(""))
return firebaseUser.getDisplayName();
if(name != null && !name.equals(""))
return name;
return "";
}
public void setName(String name){
this.name = name;
}
public String getEmail(){
if(firebaseUser.getEmail() != null && firebaseUser.getEmail().equals(""))
return firebaseUser.getEmail();
if(email != null && email.equals(""))
return email;
return "";
}
public void setEmail(String email){
this.email = email;
}
public void setFirebaseUser(FirebaseUser firebaseUser){
this.firebaseUser = firebaseUser;
}
public FirebaseUser getFirebaseUser(){
return firebaseUser;
}
public void setLastOnline(String lastOnline){
this.lastOnline = lastOnline;
}
public String getLastOnline(){
if(lastOnline != null)
return lastOnline;
return "";
}
public void setLatLong(LatLng latlng){
this.latLng = latlng;
}
public LatLng getLatLong(){
return this.latLng;
}
public ArrayList<SwapLocation> getLocations(){
return locations;
}
public void setLocations(ArrayList<SwapLocation> locations){
this.locations = locations;
}
}
但是,当尝试执行“User this_User=dataSnapshot.getValue(User.class)”时,我得到一个错误,它说“用户缺少一个没有参数的构造函数”
任何帮助都会很好,谢谢您一定收到了此错误,因为您的用户类是一个子类 解决方案: 将类标记为静态:
public class SomeParentClass{
public static class User {
....
}
}
或在单独的文件中创建它。我在kotlin中使用firebase实时数据库解决了这个问题 对于Kotlin,如果您对firebase使用下面这样的数据类
data class UserModel(
var UserName: String,
var UserType: Int
)
然后会出现如下错误
com.google.firebase.database.DatabaseException:UserModel没有
定义一个无参数构造函数。如果您正在使用ProGuard,请确保
这些构造函数没有被剥离
因此,您需要初始化它的值
data class UserModel(
var UserName: String = "",
var UserType: Int = 0
)
用户无参数错误意味着即使您确实需要空构造函数:
public User(){ }
您还需要另一个构造函数,其中的变量是构造函数内部Firebase中对象的子对象。例如:
public User(
String name,
String email,
FirebaseUser firebaseUser,
String lastOnline,
LatLng latLng,
ArrayList<SwapLocation> locations
){
this.name = name;
this.email = email;
this.firebaseUser = firebaseUser;
this.lastOnline = lastOnline;
this.latLong = latLong;
this.locations = locations;
}
@Keep
@IgnoreExtraProperties
open class User(
var name : String? = null,
var email : String? = null,
var firebaseUser : FirebaseUser? = null,
var lastOnline : String? = null,
var latLng : LatLng? = null,
var locations : ArrayList<SwapLocation>? = null
)
希望有帮助 在kotlin中,需要初始化数据类构造函数变量来发现此错误
data class Images(val imageOne:String="",
val imageTwo:String="",
val imageThree:String=""
)
我无法解释为什么会出现“缺少构造函数”错误。但如果
FirebaseUse
r不是您定义的简单POJO,而是com.google.Firebase.auth.FirebaseUser
,我怀疑Firebase能否序列化/反序列化您的User
类。该类不是一个简单的POJO,几乎可以肯定它具有Firebase序列化程序无法处理的功能。快速猜测是您的User
类是在另一个类中定义的。如果是这种情况,请将其标记为static:static public class User{
。此外,正如qbix评论的那样,我怀疑FirebaseUser
是否满足序列化的数据库要求(如果满足,那会有点酷),因此您必须@排除该项。啊,该死,这太糟糕了,因为我只需要一个用户对象。似乎我需要两个对象,一个FirebaseUser和一个用于附加属性的用户(因为用户可以在没有第三方身份验证的情况下登录,所以这些详细信息,例如姓名、电话等,需要存储在“用户”树中的某个位置)。或者,另一种方法是将FirebaseUser存储在我的Users对象中,并在调用Firebase时将其从User对象中剥离。。嗯,你们认为最好的解决方案是什么?这为我解决了这个问题。错误消息很棘手,因为无论您的构造函数是否包含一个不带任何类y参数的构造函数,您都会得到它你为数据库创建的,如果你像我最初那样在你的主要活动中一般地定义类。消息暗示类定义有问题,而不是它在哪里和如何定义的真正问题。这很弱。wtf google..真棒!为我工作
data class Images(val imageOne:String="",
val imageTwo:String="",
val imageThree:String=""
)