gpt4 book ai didi

java - DatabaseException : Can't convert object of type java. 要键入的 lang.String

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:27 26 4
gpt4 key购买 nike

我查看了其他一些类似问题的答案,但不明白为什么我的答案不起作用。

我正在尝试让我的应用程序从 Firebase 读取命令并移动无人机。 firebase 中的命令来自一个单独的软件。该应用程序基于 Parrot Drone SDK 示例代码构建。

它似乎能够从命令对象中获取文本并将其附加到 TextView ,但是当添加新的子项时它就会崩溃。添加新 child 时出现此错误。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parrot.sdksample, PID: 10592
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.parrot.sdksample.activity.CommandObject
at com.google.android.gms.internal.zg.zzb(Unknown Source)
at com.google.android.gms.internal.zg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.parrot.sdksample.activity.MiniDroneActivity$13.onChildAdded(MiniDroneActivity.java:383)
at com.google.android.gms.internal.px.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.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:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)

我在 firebase 中的数据结构示例如下。

{
"drones" : {
"commands" : {
"-L-n9HwaQktOdI2VEVlH" : {
"executed" : false,
"text" : "TAKE_OFF",
"timestamp" : 1.512686825309134E9
},
"-L-nAuK5Ifde7Cdnan8K" : {
"executed" : false,
"text" : "LAND",
"timestamp" : 1.512687248764272E9
}
}
}
}

我的 Activity 中从 firebase 获取数据的函数如下所示。

private void initFirebase(){
mCommandTextView = (TextView) findViewById(R.id.commandTextView);

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference commandsRef = database.getReference("drones/commands");

ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
CommandObject command = dataSnapshot.getValue(CommandObject.class);
mCommandTextView.append(command.text + "\n");
// I've tried commenting out the if statements below this, but it doesn't seem to make a difference.
if ("TAKE_OFF".equals(command.text)) {
mMiniDrone.takeOff();
} else if ("LAND".equals(command.text)) {
mMiniDrone.land();
}
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName){

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName){

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
commandsRef.addChildEventListener(childEventListener);
}

我的 CommandObject 类看起来像这样。

public class CommandObject {
public String text;
public float timestamp;
public boolean executed;

public CommandObject() {
}

public CommandObject(boolean executed, String text, float timestamp){
this.executed = executed;
this.text = text;
this.timestamp = timestamp;
}
}

我也尝试过使用值事件监听器,但出现了同样的问题。

最佳答案

您遇到此错误:

Can't convert object of type java.lang.String to type com.parrot.sdksample.activity.CommandObject

因为您正在尝试读取类型为 CommandObjectString 数据,这就是您收到此错误的原因。

获取这些值的更简单方法是像这样使用 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.512686825309134E9
false / LAND / 1.512687248764272E9

关于java - DatabaseException : Can't convert object of type java. 要键入的 lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47705534/

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