Can't convert object of type java.lang.String to type com.parrot.sdksample.activity.CommandObject
因为您正在尝试读取类型
String为type 的数据,
CommandObject以及为什么收到此错误的原因。
获取这些值的一种更简单的方法是使用如下所示的
String类:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();DatabaseReference commandsRef = rootRef.child("drones").child("commands");ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot ds : dataSnapshot.getChildren()) { boolean executed = ds.child("executed").getValue(Boolean.class); String text = ds.child("text").getValue(String.class); double timestamp = ds.child("timestamp").getValue(Double.class); Log.d("TAG", executed + " / " + text + " / " + timestamp); } } @Override public void onCancelled(DatabaseError databaseError) {}};commandsRef.addListenerForSinglevalueEvent(eventListener);
而这种使用
CommandObject类对象的方法:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();DatabaseReference commandsRef = rootRef.child("drones").child("commands");ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot ds : dataSnapshot.getChildren()) { CommandObject commandObject = ds.getValue(CommandObject.class); Log.d("TAG", commandObject.getExecuted() + " / " + commandObject.getText() + " / " + commandObject.getTimestamp()); } } @Override public void onCancelled(DatabaseError databaseError) {}};commandsRef.addListenerForSinglevalueEvent(eventListener);
在这两种情况下,您的输出均为:
false / TAKE_OFF / 1.512686825309134E9false / LAND / 1.512687248764272E9
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)