firebase获取错误中数据更改的数据检索

unhi4e5o  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(688)

我正在做一个基于firebase的android应用程序。我的日志显示数据更改错误。代码如下:

myRef.child("first").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                    model model = childSnapshot.getValue(model.class);
                    if (dataSnapshot.getValue() == null) {
                       Toast.makeText(getApplicationContext(),"Sorry",Toast.LENGTH_SHORT).show();
                    } else {

                        String name = model.getFb01name();

                        tvName.setText(name);

                    }

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }

        });

有人能改进吗?
这是我的logcat错误消息:它显示了这一行的错误。

model model = childSnapshot.getValue(model.class);

“在我的\项目\名称.sheetactivity$3.ondatachange(sheetactivity。java:156)"

2020-11-23 17:41:35.403 5585-5585/my_project_name E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my_project_name, PID: 5585
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type my_project_name.Model.model
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
        at my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:156)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我试了很多。有时它工作了,但又停止了。我不能指出错误在哪里。
我的模型课如下:

public class model {
    private String fb01name;

    public model() {
    }

    public model(String fb01name) {
        this.fb01name = fb01name;

    }

    public String getFb01name() {
        return fb01name;
    }

    public void setFb01name(String fb01name) {
        this.fb01name = fb01name;
    }

}

我的火基结构是这样的:

这是应用建议后的最新logcat消息:

2020-11-24 00:40:06.783 12399-12399/my_project_nameE/AndroidRuntime: FATAL EXCEPTION: main
    Process: my_project_name, PID: 12399
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
        at my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:155)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
i5desfxk

i5desfxk1#

在这里试试这个,如果您的模型类只有一个参数,这应该是好的。如果需要数据库中的所有参数,则需要从数据库中提取所有参数,然后将其添加到模型中。

myRef.child("first").addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      String name = (String) dataSnapshot.child("fb01name").getValue().toString;
        if(name == null){

            Toast.makeText(getApplicationContext(),"Sorry",Toast.LENGTH_SHORT).show();
            return;

        }

        model model = new model(name);
        tvName.setText(name);

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }

});
4ngedf3f

4ngedf3f2#

自从你读了 first ,的 DataSnapshot 你能进去吗 onDataChange 包含该位置的数据。然后循环快照中的子对象,这样 childSnapshot 变量然后指向 first (所以 fb00max , fb01mark1 等)。当你试着读一本 model 对象,它将作为值失败 10 不能解释为 model .
你要么从树的更高一级读取,要么从你的文件中删除循环 onDataChange .

从树的更高一级读取

myRef.addValueEventListener(new ValueEventListener() {
    ...

从ondatachange中删除循环

myRef.child("first").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        model model = dataSnapshot.getValue(model.class);
        if (dataSnapshot.getValue() == null) {
           Toast.makeText(getApplicationContext(),"Sorry",Toast.LENGTH_SHORT).show();
        } else {
            String name = model.getFb01name();

            tvName.setText(name);                    
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // Never ignore errors
    }
});

相关问题