Firebase存储数据库中的自定义对象NaN错误

时间:2018-04-01 09:24:57

标签: android firebase firebase-realtime-database

我试图存储我的" Person" firebase数据库中的对象,但是我得到了关于NaN值的相同错误。

这是Person类:

package HelperObjects;

import android.media.Image;

import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.database.IgnoreExtraProperties;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 13/03/2018.
 */
@IgnoreExtraProperties
public class Person {
    private String name;
    private String personImage;


    private String phoneNumber;

    public LatLng getAddress() {
        return address;
    }

    public void setAddress(LatLng address) {
        this.address = address;
    }

    private LatLng address;
    private String email;

    public String getDevice_token() {
        return device_token;
    }

    public void setDevice_token(String device_token) {
        this.device_token = device_token;
    }

    private String device_token;
    private int age;
    private String gender;
    private List<Job> listOfJobs = new ArrayList<Job>();
    private List<Feedback> listOfFeedbacks = new ArrayList<Feedback>();

    public Person(){

    }


    /**
     * inner class of Feedback
     */
    private class Feedback {
        private double myRate;
        private String myComment;

        public Feedback(double rate, String comment) {
            myRate = rate;
            myComment = comment;
        }
    }

    /**
     * find the current rate (average).
     * @return
     */
    public double getCurrnetFeedbackRate() {
        double sum = 0;
        for (Feedback feedback : listOfFeedbacks) {
            sum += feedback.myRate;
        }
        return  sum/listOfFeedbacks.size();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

     public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPersonImage() {
        return personImage;
    }

    public void setPersonImage(String personImage) {
        this.personImage = personImage;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public List<Job> getListOfJobs() {
        return listOfJobs;
    }

    public void setListOfJobs(List<Job> listOfJobs) {
        this.listOfJobs = listOfJobs;
    }
}

这是我的设定值方法:

GraphRequest request = GraphRequest.newMeRequest(
                accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // Application code
                        try {
                            Log.i("Response",response.toString());
                            Person person = new Person();
                            person.setEmail(response.getJSONObject().getString("email"));
                            person.setName(response.getJSONObject().getString("first_name") + " " +
                                    response.getJSONObject().getString("last_name"));
                            person.setGender(response.getJSONObject().getString("gender"));;
                            person.setDevice_token(FirebaseInstanceId.getInstance().getToken());;

                            mDatabase.setValue(person).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if(task.isSuccessful()){
                                        Log.i(" mDatabase.setValue", "task.isSuccessful");
                                    }

                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

这就是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.rubz.dvir.rubz, PID: 27287
                  com.google.firebase.database.DatabaseException: Invalid value: Value cannot be NaN, Inf or -Inf.
                      at com.google.android.gms.internal.zzepf.zza(Unknown Source:200)
                      at com.google.android.gms.internal.zzepf.zza(Unknown Source:141)
                      at com.google.firebase.database.DatabaseReference.zza(Unknown Source:14)
                      at com.google.firebase.database.DatabaseReference.setValue(Unknown Source:7)
                      at com.rubz.dvir.rubz.MainCustomerActivity$3.onCompleted(MainCustomerActivity.java:189)
                      at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:301)
                      at com.facebook.GraphRequest$5.run(GraphRequest.java:1380)
                      at com.facebook.GraphRequest.runCallbacks(GraphRequest.java:1393)
                      at com.facebook.GraphRequest.executeConnectionAndWait(GraphRequest.java:1280)
                      at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1165)
                      at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1131)
                      at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1115)
                      at com.facebook.GraphRequest.executeAndWait(GraphRequest.java:1090)
                      at com.facebook.GraphRequest.executeAndWait(GraphRequest.java:984)
                      at com.rubz.dvir.rubz.MainCustomerActivity.registerNewUser(MainCustomerActivity.java:214)
                      at com.rubz.dvir.rubz.MainCustomerActivity.access$200(MainCustomerActivity.java:55)
                      at com.rubz.dvir.rubz.MainCustomerActivity$2.onComplete(MainCustomerActivity.java:147)
                      at com.google.android.gms.tasks.zzf.run(Unknown Source:23)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6938)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.

Person对象同时包含Feedback和Job列表,这两个对象都是非平凡的对象。 那是什么原因?

有没有办法将对象存储在数据库中? (例如,两个列表都有空值?)

非常感谢!

编辑:

我已将Feedback类移动到一个单独的类,如果我注释掉反馈列表,代码可以正常工作,我可以在firebase中看到数据,但是反馈列表会中断。
奇怪的部分是作业列表很好,代码编译并运行它(虽然对我来说它们看起来完全相同)

0 个答案:

没有答案