如何在Android的firebase中存储和检索对象?

时间:2016-08-24 10:20:06

标签: java android firebase firebase-realtime-database

所以我有一个用户类。并且用户类具有称为关注者的用户的arraylist。当我尝试上传一个用户对象(该用户对象的关注者arraylist中有少数用户)Firebase无法执行此操作而我的程序崩溃...我无法计算如何存储和检索我自己的对象,其中包含其他自定义对象。

所以我得到的主要错误是:

无法启动活动ComponentInfo {com.daprlabs.swipedeck / com.daprlabs.swipedeck.ActivityCenter}:com.firebase.client.FirebaseException:无法解析为快照

这是我的java代码。

编辑:添加@exclude,但仍然无法正常工作......

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRef = new Firebase("Fire base reference...");
        dummyUser();
    }

    private void dummyUser()
    {
        //Andie <- Chris <- Qamar
        mainDummyUser = new User();
        mainDummyUser.setName("Chris");
        mainDummyUser.setLocation("Deutchland");
        mainDummyUser.setDummyImage(R.drawable.chris);
        //Second Dummy
        User secondDummy = new User();
        secondDummy.setName("Andie");
        secondDummy.setLocation("Germany");
        secondDummy.setDummyImage(R.drawable.andie);
        secondDummy.addtoFollowers(mainDummyUser);

        mainDummyUser.addtoFollowers(secondDummy);

        //Add users to Firebase
        mRef.child("users").child("Dummy User 1").setValue(mainDummyUser);


    }

这是我的用户类,非常简单。

public class User
{
    private String userName;
    private int userID;
    private String userEmail;
    private String userProfilePicture;
    private ArrayList<CityFolders> userFolders = new ArrayList<>();

    //Constructor
    public User()
    {
        setDummyUserID();
    }
    public void addtoFollowers(User user)
    {
        userFollowers.add(user);
    }
    public void setLocation(String location)
    {
        this.location = location;
    }
    public void setDummyImage(int image)
    {
        this.dummyImage = image;
    }

     public void setDummyUserID()
    {
        Random randomNumberGenerator = new Random();
        userID = randomNumberGenerator.nextInt(1000000);
    }
    //Getter
    public String getUserName()
    {
       return userName;
    }


    public String getLocation()
    {
        return location;
    }
    public int getUserID()
    {
        return userID;
    } 
    @Exclude
    public ArrayList<UserFinal> getUserFollowers()
    {
       return userFollowers;
    }
    //Goes on...
}

这是我的堆栈跟踪:

08-24 19:07:26.549 23384-23384/com.daprlabs.swipedeck E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.daprlabs.swipedeck, PID: 23384
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daprlabs.swipedeck/com.daprlabs.swipedeck.ActivityCenter}: com.firebase.client.FirebaseException: Failed to parse to snapshot
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:157)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                         Caused by: com.firebase.client.FirebaseException: Failed to parse to snapshot
                                                                            at com.firebase.client.Firebase.setValueInternal(Firebase.java:357)
                                                                            at com.firebase.client.Firebase.setValue(Firebase.java:248)
                                                                            at com.daprlabs.swipedeck.ActivityCenter.dummyUser(ActivityCenter.java:165)
                                                                            at com.daprlabs.swipedeck.ActivityCenter.onCreate(ActivityCenter.java:122)
                                                                            at android.app.Activity.performCreate(Activity.java:6272)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:157) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5525) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
                                                                         Caused by: java.lang.IllegalArgumentException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.Arra
08-24 19:07:26.577 23384-23394/com.daprlabs.swipedeck W/art: Suspending all threads took: 32.173ms

1 个答案:

答案 0 :(得分:1)

如何使用User而不是UserID? 像

    private void dummyUser()
    {
        mainDummyUser = new User();
        mainDummyUser.setName("Chris");
        mainDummyUser.setLocation("Deutchland");

        User secondDummy = new User();
        secondDummy.setName("Andie");
        secondDummy.setLocation("Germany");

        secondDummy.addFollowerUserIDs(mainDummyUser.getUserID());
        mainDummyUser.addFollowerUserIDs(secondDummy.getUserID());
        //Add users to Firebase
        mRef.child("users").child(mainDummyUser.getUserID()).setValue(mainDummyUser);
        mRef.child("users").child(secondDummy.getUserID()).setValue(secondDummy);
    }

public class User {

    private String userName;
    private String location;
    private String userID;
    private ArrayList<String> FollowerUserIDs = new ArrayList<>();

    //Constructor
    @SuppressWarnings("unused")
    public User()
    {
        setDummyUserID();
    }
    public void addFollowerUserIDs(String userID)
    {
        FollowerUserIDs.add(userID);
    }

    public void setLocation(String location)
    {
        this.location = location;
    }
    public void setDummyImage(int image)
    {
    // this.dummyImage = image;
    }

    public void setDummyUserID()
    {
        Random randomNumberGenerator = new Random();
        userID = String.valueOf(randomNumberGenerator.nextInt(1000000));
    }

    public void setUserID()
    {
        Random randomNumberGenerator = new Random();
        userID = String.valueOf(randomNumberGenerator.nextInt(1000000));
    }
    //Getter
    public String getUserName()
    {
        return userName;
    }
    public void setName(String userName)
    {
        this.userName = userName;
    }


    public String getLocation()
    {
        return location;
    }
    public String getUserID()
    {
        return userID;
    }

    public ArrayList<String> getFollowerUserIDs()
    {
        return FollowerUserIDs;
    }
}

此外,您的示例代码是

public class User
{
    private String userName;
    private int userID;
    private String userEmail;
    private String userProfilePicture;
    private ArrayList<CityFolders> userFolders = new ArrayList<>();.

但它应该是

public class User
{
    private String userName;
    private int userID;
    private String userEmail;
    private String userProfilePicture;
    private ArrayList<User> userFollowers = new ArrayList<>();