[英]Firebase Database: Getting “No-Argument Constructor” Error Despite Having It
I am trying to writing data in an activity and then reading it in another activity. 我试图在一个活动中写入数据,然后在另一个活动中读取数据。 My writing part is working.
我的写作部分正在工作。 For my reading part, I am trying to use getValue with parameter of a class I wrote.
对于我的阅读部分,我试图将getValue与我编写的类的参数一起使用。 But I keep getting this error:
但我不断收到此错误:
com.google.firebase.database.DatabaseException: Class ozhan.climb.MainActivity$User does not define a no-argument constructor.
com.google.firebase.database.DatabaseException:类ozhan.climb.MainActivity $ User没有定义无参数构造函数。 If you are using ProGuard, make sure these constructors are not stripped.
如果您使用的是ProGuard,请确保未删除这些构造函数。
My class that used in getValue() : 我在getValue()中使用的类:
class User {
public String Server,
NeededRole,
Role,
SummonerName;
User() {}
public String get_Server() {
return Server;
}
public String get_NeededRole() {
return NeededRole;
}
public String get_Role() {
return Role;
}
public String get_SummonerName() {
return SummonerName;
}
}
and here is the code that should get data: 这是应该获取数据的代码:
public void getUserData(){
final TextView tv_sumName = findViewById(R.id.tv_sumName),
tv_neededRole = findViewById(R.id.tv_neededrole),
tv_userRole = findViewById(R.id.tv_userrole),
tv_server = findViewById(R.id.tv_server);
final String uid = Objects.requireNonNull(mAuth.getCurrentUser()).getUid();
DatabaseReference usersDataRef = databaseRef.child("Users").child(uid);
usersDataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User u = dataSnapshot.getValue(User.class);
if (u != null) {
tv_server.setText(u.Server);
tv_neededRole.setText(u.NeededRole);
tv_userRole.setText(u.Role);
tv_sumName.setText(u.SummonerName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
The exception says your class is ozhan.climb.MainActivity$User
- ie, it is an inner class of MainActivity
. 例外情况是您的类为
ozhan.climb.MainActivity$User
即它是MainActivity
的内部类。
Inner classes require an instance of their outer class to construct. 内部类需要构造其外部类的实例。 This won't work with Firebase Database, although the error message could definitely be improved.
尽管错误消息肯定可以得到改善,但这不适用于Firebase数据库。
Your User class should be a static
class, which does not have the same requirement to exist only within the instance of a containing class: 您的User类应该是一个
static
类,它没有相同的要求仅在包含类的实例中存在:
static class User {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.