gpt4 book ai didi

java - 检索子值失败-Firebase-Android

转载 作者:行者123 更新时间:2023-12-02 08:53:12 25 4
gpt4 key购买 nike

我尝试使用 Datasnapshot 添加每个列表的成本,然后将其发送到 firebase,并再次使用发送的数据到另一个 Activity 中,但我的应用程序打开然后崩溃并抛出以下错误。

    2020-03-12 14:43:13.887 17284-17284/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.zfr, PID: 17284
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@19.2.1:425)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:216)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203)
at com.example.android.zfr.NavActivity$2.onDataChange(NavActivity.java:71)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.2.1:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

这是我使用数据的 Activity 代码

nDb=database.getReference().child("Navigation Activity").child("Cost");
nDb.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String cost=dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + cost);
nav_total_cost.setText(cost);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

请不要因为我的错误而评判我。

最佳答案

以这种方式更改您的代码

nDb=database.getReference().child("Navigation Activity").child("Cost");
nDb.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
Log.d(TAG, "Value is: " + cost);
nav_total_cost.setText(cost.toString());
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

您正在转换错误的值,您的结果是 Long 并且您正在转换为 String

关于java - 检索子值失败-Firebase-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60651159/

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