Firebase数据库:获取“无参数构造函数"尽管有错误 [英] Firebase Database: Getting "No-Argument Constructor" Error Despite Having It
问题描述
我试图在一个活动中写入数据,然后在另一个活动中读取数据.我的写作部分正在工作.对于我的阅读部分,我试图将getValue与我编写的类的参数一起使用.但我不断收到此错误:
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. But I keep getting this error:
com.google.firebase.database.DatabaseException:类ozhan.climb.MainActivity $ User没有定义无参数构造函数.如果您使用的是ProGuard,请确保没有剥离这些构造函数.
com.google.firebase.database.DatabaseException: Class ozhan.climb.MainActivity$User does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
我在 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) {
}
});
}
推荐答案
该异常表明您的类是 ozhan.climb.MainActivity $ User
-即,它是 MainActivity的内部类
.
The exception says your class is ozhan.climb.MainActivity$User
- i.e., it is an inner class of MainActivity
.
内部类需要构造其外部类的实例.尽管错误消息肯定可以得到改善,但这不适用于Firebase数据库.
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.
您的User类应该是 static
类,该类没有相同的要求,即仅在包含类的实例中存在:
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:
static class User {
...
}
这篇关于Firebase数据库:获取“无参数构造函数"尽管有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!