简体   繁体   English

Firebase数据库:尽管存在错误,但仍出现“无参数构造函数”错误

[英]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.

相关问题 Android Firebase数据库异常:未定义无参数构造函数 - Android Firebase Database exception: not define a no-argument constructor 无参构造函数调用2参数构造函数 - No-argument constructor calling 2-argument constructor Exception或其子类中的无参数构造函数 - No-argument constructor in Exception or its subclasses 隐式调用超类中的无参数构造函数 - Calling no-argument constructor in superclass implicitly 强制超类包含无参数构造函数 - Force superclasses to include a no-argument constructor 在编译时强制存在无参数构造函数(Java) - Enforce presence of no-argument constructor at compile time (Java) 为无参数构造函数的类创建动态代理 - Create a dynamic proxy for a class without no-argument constructor 像这样的非静态内部类只能通过在反序列化时使用默认的无参数构造函数实例化错误 - Non-static inner classes like this can only by instantiated using default, no-argument constructor error when deserialize Scala类的最终def this()声明没有公共的无参数构造函数 - Scala class final def this() declares no public no-argument constructor 即使子类已经有一个已定义的构造函数,父类是否总是需要一个默认或无参数的构造函数? - Does a parent class always need a default or no-argument constructor even if the child class already has a defined constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM