【问题标题】:Firebase Android client: "in object can not be apply to java.lang.class"Firebase Android 客户端:“in object can not be apply to java.lang.class”
【发布时间】:2017-07-12 08:26:50
【问题描述】:

我决定使用安卓查询数据库,而不是使用云端函数。

所以我正在尝试运行查询

Query personQuery = myRef.orderByChild("mCalculateFaceSizeWidth")
                .startAt(mCalculateFaceSizeWidth())
                .limitToFirst(1);
        personQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
                Person person = dataSnapshot.getValue().getClass(Person.class);
            }
....

这是我得到错误的地方

    Person person = dataSnapshot.getValue().getClass(Person.class);

关于 getClass(Person.class)

The error is: "in object can not be apply to java.lang.class<com.example.erang.facerecognition.Person>

这是我的 Person 类。

package com.example.erang.facerecognition;


import org.json.JSONObject;

import java.lang.reflect.Array;

/**
 * Created by erang on 12-Jul-17.
 */
public class Person {

    public String name;
    public int age;
    public String id;
    public Array children;
    public String address;
    public String image;
    public JSONObject faceDetails;

    public Person() {
        // Default constructor required for calls to DataSnapshot.getValue(Person.class)
    }


    public Person(String name,int age,String id,Array children,String address,String image,JSONObject faceDetails){

        this.name = name;
        this.age = age;
        this.id = id;
        this.children = children;
        this.address = address;
        this.image = image;
        this.faceDetails = faceDetails;

    }

    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }

    public String getID(){
        return id;
    }
    public Array getChildren(){
        return children;
    }
    public String getAddress(){
        return address;
    }
    public String getImage(){
        return image;
    }
    public JSONObject getFaceDetails(){
        return faceDetails;
    }

}

这是我加载数据库的 json 文件。

{
"People":{
    "EranGross":{
    "name":"Eran Gross",
    "age":42,
    "id":"032208373",
    "children":["Nadav","Amit"],
    "address":"Aharon Boxer 38 Ness Ziona",
    "image":"https://firebasestorage.googleapis.com/v0/b/facerecognition-29c9e.appspot.com/o/IMG_20170403_112227_641.jpg?alt=media&token=9523d69d-a61c-47b8-90b8-9f4b0c635d7d",
    "faceDetails":{
    "mCalculateFaceSizeHeight" : 121,
    "mCalculateFaceSizeWidth" : 107,
    "mCalculateLeftEyeBrowSizeHeight" : 31,
    "mCalculateLeftEyeBrowSizeWidth" : 43,
    "mCalculateLeftEyeSizeHeight" : 64,
    "mCalculateLeftEyeSizeWidth" : 14,
    "mCalculateMouthSizeHeight" : 24,
    "mCalculateMouthSizeWidth" : 30,
    "mCalculateNoseSizeHeight" : 43,
    "mCalculateNoseSizeWidth" : 71,
    "mCalculateRightEyeBrowSizeHeight" : 43,
    "mCalculateRightEyeBrowSizeWidth" : 52,
    "mCalculateRightEyeSizeWidth" : 14,
    "mCalculatedRightEyeSizeHeight" : 36
            }
        },
    "DavidWebb":{
    "name":"David Webb",
    "age":42,
    "id":"414222333",
    "children":["Nathan","Jason"],
    "Address":"31 street San Jose",
    "image":"https://firebasestorage.googleapis.com/v0/b/facerecognition-29c9e.appspot.com/o/IMG_20170403_112227_641.jpg?alt=media&token=9523d69d-a61c-47b8-90b8-9f4b0c635d7d",
    "faceDetails":{
    "mCalculateFaceSizeHeight" : 114,
    "mCalculateFaceSizeWidth" : 103,
    "mCalculateLeftEyeBrowSizeHeight" : 25,
    "mCalculateLeftEyeBrowSizeWidth" : 22,
    "mCalculateLeftEyeSizeHeight" : 50,
    "mCalculateLeftEyeSizeWidth" : 14,
    "mCalculateMouthSizeHeight" : 24,
    "mCalculateMouthSizeWidth" : 20,
    "mCalculateNoseSizeHeight" : 35,
    "mCalculateNoseSizeWidth" : 68,
    "mCalculateRightEyeBrowSizeHeight" : 35,
    "mCalculateRightEyeBrowSizeWidth" : 45,
    "mCalculateRightEyeSizeWidth" : 10,
    "mCalculatedRightEyeSizeHeight" : 26
            }
        }

    }

}

【问题讨论】:

  • myRef 指向什么?
  • DatabaseReference myRef = database.getReference("People");
  • database 指向?请添加您的DatabaseReference 的完整声明。

标签: android firebase firebase-realtime-database firebase-storage


【解决方案1】:

改变这个

Person person = dataSnapshot.getValue().getClass(Person.class);

Person person = dataSnapshot.getValue(Person.class);

您的方法不起作用的原因是 getValue().getClass() 返回对象的运行时 Class 而不是对象。你可以阅读更多关于它的信息here

【讨论】:

    【解决方案2】:

    试试

    public class RootPerson {
    
    public Person person;
    
    public class Person {
    
        public String name;
        public int age;
        public String id;
        public Array children;
        public String address;
        public String image;
        public JSONObject faceDetails;
    
        public Person() {
            // Default constructor required for calls to DataSnapshot.getValue(Person.class)
        }
    
    
        public Person(String name,int age,String id,Array children,String address,String image,JSONObject faceDetails){
    
            this.name = name;
            this.age = age;
            this.id = id;
            this.children = children;
            this.address = address;
            this.image = image;
            this.faceDetails = faceDetails;
    
        }
    
        public String getName(){
            return name;
        }
        public int getAge(){
            return age;
        }
    
        public String getID(){
            return id;
        }
        public Array getChildren(){
            return children;
        }
        public String getAddress(){
            return address;
        }
        public String getImage(){
            return image;
        }
        public JSONObject getFaceDetails(){
            return faceDetails;
        }
    
    }
    }
    

    Query personQuery = myRef.orderByChild("mCalculateFaceSizeWidth")
                .startAt(mCalculateFaceSizeWidth())
                .limitToFirst(1);
        personQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
                RootPerson person = dataSnapshot.getValue().getClass(RootPerson.class);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-04
      • 2022-01-12
      • 1970-01-01
      • 2022-12-28
      • 2022-12-02
      • 2018-07-28
      • 2022-12-02
      相关资源
      最近更新 更多