gpt4 book ai didi

java - 从 Firebase 接收对象不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:38 25 4
gpt4 key购买 nike

我正在尝试从我的 Firebase 数据库接收服装对象。我总是遇到异常,但我不明白为什么......

------- beginning of crash 06-17 08:26:56.208 13875-13875/com.example.jendrik.moerder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jendrik.moerder, PID: 13875
com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.example.jendrik.moerder.GameObjekts.Clue
at com.google.android.gms.internal.zzaix.zzd(Unknown Source)
...a lot of double errors...
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) at com.example.jendrik.moerder.FCM.FCMListeners.updateGame(FCMListeners.java:64) at com.example.jendrik.moerder.FCM.FCMListeners.access$000(FCMListeners.java:26) at com.example.jendrik.moerder.FCM.FCMListeners$1.onDataChange(FCMListeners.java:105) at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzafp.zza(Unknown Source)
at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
at com.google.android.gms.internal.zzags$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我将对象加载到数据库中的方式:

public void sendGame(Game game){
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
myRef.child("games").child(gameName).setValue(game);
}

这是我接收它的方式:

 public FCMListeners(String gameName, GameStartedCallback callback){

this.gameName=gameName;
this.callback = callback;
gameReference = FirebaseDatabase.getInstance().getReference().child("games").child(gameName);
gameListener = makeGameListener();
gameReference.addListenerForSingleValueEvent(gameListener);
}



public ValueEventListener makeGameListener(){

ValueEventListener ve =
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
updateGame(dataSnapshot);
callback.startGameAfterReceivingInformation();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};

return ve;
}

private void updateGame(DataSnapshot snapshot){
Log.d("tollewurst",snapshot.getKey());
Game game=snapshot.getValue(Game.class);
this.game=game;
}

public Game getGame(){
return game;
}

从正在运行的类中,我调用 FCMListener 来接收:

 private FCMListeners fcmListeners;

private void startGame(){
myRef.removeEventListener(el);
gamestarter.removeEventListener(elGame); //these are listeners for other informations who are not used anymore at this point

fcmListeners = new FCMListeners(gameName, this);
}

public void startGameAfterReceivingInformation(){
Intent intent = new Intent(this,MenueDrawer.class);
intent.putExtra("gameName", gameName);
intent.putExtra("whoAmI", checkForPlayerNumber());

Game game = fcmListeners.getGame();
intent.putExtra("GAME", game);
Log.d("start",game.getGameName());

startActivity(intent);
}

我的回调接口(interface):

public interface GameStartedCallback {
void startGameAfterReceivingInformation();
}

这是我的 Game.class - 接收对象的类:

@Getter @Setter
@IgnoreExtraProperties
public class Game implements Serializable {

private Solution solution;
private RoomManager roomManager;
private WeaponManager weaponManager;
private PlayerManager playerManager;
private List<Clue> clueList;
private double numberOfThings;
private String gameName;
private String pwd;
private double min;
private double sec;
private double justScannedQR;
private double playerAmount;
private boolean gameOver;
private boolean isRunning;

public Game(String gameName, String pwd, ArrayList<String> rooms, ArrayList<String> weapons, int min, int sec, int playerAmount){
this.gameName = gameName;
this.pwd = pwd;
this.min = min;
this.sec = sec;
solution = null;
clueList = new ArrayList<>();
playerManager = new PlayerManager(false);
weaponManager = new WeaponManager(false);
roomManager = new RoomManager(false);
createRooms(rooms);
createWeapons(weapons);
numberOfThings = rooms.size() + weapons.size();
justScannedQR = 0;
this.playerAmount = playerAmount;
gameOver = false;
}

public Game(){} //and then a lot of methods who are @Exclude

由于错误对话框中的“未知路径”部分,我已经尝试过以不同的方式切换子节点。毫不奇怪,没有任何运气。

奇怪的是,在其他方面,我对列表和单个值没有任何问题(目前大多数 boolean 值)。我猜我的 Game.class 有问题

如果有人能帮助我,那就太好了。我希望这些信息已经足够了。

几天后我找到了解决方案......在清理代码时,我在不同的类中发现了一个 fragment :

public void setGivenClues(Clue clue){ givenClues.add(clue); }

由于这个 setter ,应用程序崩溃了。在我的玩家对象(游戏对象的一部分)内部是一个线索列表。该 setter 应在列表中添加单个线索。但 Firebase 将其解释为整个列表的 setter。重命名我的 setter 并更改调用代码后,一切都很好!

最佳答案

在 Firebase 9.x 版本中,您可以使用 @Exclude 来忽略以“get”开头的方法。

关于java - 从 Firebase 接收对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com