尝试将Firebase.CompletionListener添加到我的ValueEventListener时出现此错误。

com.google.firebase.database.DatabaseException: Failed to parse node with class class c.kristofer.jaxx.MainActivity.MainActivity$2$1$1
   at com.google.android.gms.internal.firebase_database.zzjd.zza(Unknown Source)
   at com.google.android.gms.internal.firebase_database.zzjg.zzc(Unknown Source)
   at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
   at c.kristofer.jax2.MainActivity.MainActivity$2$1.onDataChange(MainActivity.java:110)
   at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
   at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
   at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6682)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)


这是我从here获得的代码:

fromPath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(uid)
                         .child(uid);
toPath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(deeplink.getQueryParameter("groupUid"))
                         .child(uid);
userIdPath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(deeplink.getQueryParameter("groupUid"))
                         .child(uid)
                         .child("uid");
deletePath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(uid);
fromPath.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        toPath.setValue(dataSnapshot.getValue(), new Firebase.CompletionListener() {
                                            @Override
                                            public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                                                if (firebaseError != null) {
                                                    System.out.println("Data could not be saved. " + firebaseError.getMessage());
                                                } else {
                                                    System.out.println("Data saved successfully.");
                                                    userIdPath.setValue(uid);
                                                    deletePath.removeValue();
                                                }
                                            }
                                        });
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });


当我不使用onCompleteListener时,应用程序可以正常工作,但是当我崩溃时,它会崩溃。

最佳答案

实际上,我回答了这个问题,但是当时我使用的API现在被认为是旧的。因此,我建议您使用以下方法:

private void moveRecord(DatabaseReference fromPath, final DatabaseReference toPath) {
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            toPath.setValue(dataSnapshot.getValue()).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isComplete()) {
                        Log.d(TAG, "Success!");
                    } else {
                        Log.d(TAG, "Copy failed!");
                    }
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    fromPath.addListenerForSingleValueEvent(valueEventListener);
}

08-28 21:59