将对象列表添加到Firebase

时间:2016-11-26 16:44:51

标签: java android firebase firebase-realtime-database

我正在研究一些可能会带来游戏的代码"对象并将其放入Firebase数据库。每个游戏都有一个名字和一个"玩家"每个人都有一个名字和一个"目标"。当我运行代码但是应用程序崩溃时没有明显的错误,它只是声明它无法执行该方法。我确定它与玩家列表有关,因为早些时候我可以让它与游戏名称一起使用。

public void beginGame(View view) {
    mDatabase = FirebaseDatabase.getInstance().getReference();


    EditText gameNameField = (EditText) findViewById(R.id.gameName);
    String gameName = gameNameField.getText().toString().trim();
    Collections.shuffle(players);
    if (gameName.isEmpty()) {
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(getApplicationContext(),
                R.string.toast_no_game_name, duration);
        toast.show();

    } else if (playerNames.size() < 2) {
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(getApplicationContext(),
                R.string.toast_not_enough_players, duration);
        toast.show();

    } else {
        for (int i = 0; i < players.size(); i++) {
            if (i != (players.size() - 1)) {
                players.get(i).target = players.get(i + 1);
            } else {
                players.get(i).target = players.get(0);
            }
        }
        Game game = new Game(gameName, players);
        mDatabase.child("Games").child(gameName).setValue(game);

        Intent intent = new Intent(StartNewGameActivity.this, SessionsActivity.class);
        startActivity(intent);
    }
}

要明确我希望我的数据库组织如下:

Games:
    Game1:
        name: Johns Game
        Players: 
             John
             Mike

    Game2:......

编辑1 玩家在一个对话框中初始化,该对话框要求玩家姓名,然后将玩家传递到数组列表。

public void addNewPlayer(View view) {
    DialogFragment newFragment = new CreatePlayerDialogFragment();
    newFragment.show(getSupportFragmentManager(), "Player");
}

@Override
public void onDialogPositiveClick(String name) {
    playerNames.add(name);
    players.add(new Player(name));
}

这是我的Logcat错误

11-26 12:15:29.780 10730-10730/com.example.hunte.assassin E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hunte.assassin, PID: 10730
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:21342)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5551)
        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.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5217) 
        at android.view.View$PerformClick.run(View.java:21342) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5551) 
        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.StackOverflowError: stack size 8MB
        at java.lang.reflect.Method.invoke(Native Method)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
        at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
        at com.google.android.gms.internal.zzbq
11-26 12:15:29.780 10730-10740/com.example.hunte.assassin I/art: Background sticky concurrent mark sweep GC freed 6950(796KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 136MB/136MB, paused 42.643ms total 65.881ms
11-26 12:15:30.129 1761-1821/? V/WindowOrientationListener: Proposed rotation changed! : currentRotation=0, proposedRotation=0, oldProposedRotation=-1, predictedRotation=0, timeDeltaMS=66.65039, isAccelerating=false, isFlat=false, isSwinging=false, isOverhead=false, isTouched=false, timeUntilSettledMS=0.0, timeUntilAccelerationDelayExpiredMS=0.0, timeUntilFlatDelayExpiredMS=0.0, timeUntilSwingDelayExpiredMS=0.0, timeUntilTouchDelayExpiredMS=0.0
11-26 12:15:30.188 1761-2477/? D/sensors_hal_LGMotionAccel: processInd: LP2: X: 1.027176 Y: 4.640945 Z: 8.011398 SAM TS: 153411799 HAL TS:135753777272948 elapsedRealtimeNano:135753856670678
11-26 12:15:30.311 10730-10730/com.example.hunte.assassin D/Error: ERR: exClass=java.lang.StackOverflowError
11-26 12:15:30.311 10730-10730/com.example.hunte.assassin D/Error: ERR: exMsg=stack size 8MB
11-26 12:15:30.311 10730-10730/com.example.hunte.assassin D/Error: ERR: file=Method.java
11-26 12:15:30.311 10730-10730/com.example.hunte.assassin D/Error: ERR: class=java.lang.reflect.Method
11-26 12:15:30.311 10730-10730/com.example.hunte.assassin D/Error: ERR: method=invoke line=-2
11-26 12:15:30.381 10730-10730/com.example.hunte.assassin D/Error: ERR: stack=java.lang.IllegalStateException: Could not execute method for android:onClick
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
   at android.view.View.performClick(View.java:5217)
   at android.view.View$PerformClick.run(View.java:21342)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5551)
   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.reflect.InvocationTargetException
   at java.lang.reflect.Method.invoke(Native Method)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   at android.view.View.performClick(View.java:5217) 
   at android.view.View$PerformClick.run(View.java:21342) 
   at android.os.Handler.handleCallback(Handler.java:739) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5551) 
   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.StackOverflowError: stack size 8MB
   at java.lang.reflect.Method.invoke(Native Method)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
   at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
   at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
at com.google.android.gms.internal.zzbqi
11-26 12:15:30.383 10730-10730/? D/Error: ERR: TOTAL BYTES WRITTEN: 20167208
11-26 12:15:30.383 10730-10730/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 20167300)
11-26 12:15:30.383 10730-10730/? E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException: data parcel size 20167300 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4547)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

2 个答案:

答案 0 :(得分:0)

据我所知,你不可能在一个孩子身上有很多价值观。尝试逐个设置值,每次为每个新玩家创建一个新的子项。 mDatabase.child(&#34;游戏和#34)。子(gameName).child(playerName);其中playerName被更改(i ++在数组列表中递增)并为每个项目创建新的子项。

答案 1 :(得分:0)

事实证明我没有一个准确的玩家构造函数(它实际上没有名字)但是我发布我的代码以防将来会有类似的问题

public void beginGame(View view) {
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference();

    EditText gameNameField = (EditText) findViewById(R.id.gameName);
    String gameName = gameNameField.getText().toString().trim();
    Collections.shuffle(players);
    if (gameName.isEmpty()) {
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(getApplicationContext(),
                R.string.toast_no_game_name, duration);
        toast.show();

    } else if (playerNames.size() < 2) {
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(getApplicationContext(),
                R.string.toast_not_enough_players, duration);
        toast.show();

    } else {
        for (int i = 0; i < players.size(); i++) {
            if (i != (players.size() - 1)) {
                players.get(i).target = players.get(i + 1);
            } else {
                players.get(i).target = players.get(0);
            }
        }
        Game game = new Game(gameName, players);
        DatabaseReference gamesRef = ref.child("Games").child(gameName).child("players");
        Map<String, Player> gamePlayers = new HashMap<>();
        for(int i = 0; i < players.size(); i++){
            Player currentPlayer = players.get(i);
            gamesRef.child(currentPlayer.getName()).setValue(currentPlayer);

        }

    }
}