gpt4 book ai didi

java - 火力地堡 "User is missing a constructor with no arguments"

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:11 24 4
gpt4 key购买 nike

我正在尝试从 Firebase 中的/User/树中检索我的用户详细信息。我有以下用户对象:

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;
}
}

我正在尝试从 Firebase 检索此对象。尽管在序列化到我的用户对象时出现问题,但在 dataSnapshot 中检索了该对象。

这就是我从 Firebase 中检索对象的方式:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(users_group).child(user.getUid());
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User this_user = dataSnapshot.getValue(User.class);
}

@Override
public void onCancelled(DatabaseError databaseError) { }
});

但是当尝试执行“User this_user = dataSnapshot.getValue(User.class)”时,我收到一条错误消息“User is missing a constructor with no arguments”

任何帮助都会很棒,谢谢

最佳答案

我在使用 firebase 实时数据库的 kotlin 中遇到过这个问题,并通过 this solution 解决了它

对于 Kotlin,如果您在 firebase 中使用如下数据类,

data class UserModel(
var UserName: String,
var UserType: Int
)

然后你会得到类似这样的错误

com.google.firebase.database.DatabaseException: UserModel does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.

为此,您需要初始化它的值。

data class UserModel(
var UserName: String = "",
var UserType: Int = 0
)

关于java - 火力地堡 "User is missing a constructor with no arguments",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com