英文:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
问题
以下是翻译好的部分:
我想从数据库(实时 Firebase)中检索一个值,然后将其乘以另一个变量,并将结果赋值给另一个变量。我尝试了以下的方法,但是出现了错误。有人可以帮助我吗?
private void getData(final TextView deliCha) {
rootRef.child(appId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
double Distance = (double) dataSnapshot.child("Distance").getValue();
double charge = Distance * 100.0;
deliCha.setText(String.valueOf(charge));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是错误信息。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ketomate, PID: 13156
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at com.example.ketomate.BillDetailsForPaymentAndDelivery$2.onDataChange(BillDetailsForPaymentAndDelivery.java:121)
at com.google.firebase.database.Query$1.onDataChange(Query.java:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
英文:
I want to multiply a value after retrieving it from the database(realtime firebase) and assign to another variable. I tried to do it like this, but I am getting an error. Can someone please help me?
private void getData(final TextView deliCha) {
rootRef.child(appId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
double Distance = (double) dataSnapshot.child("Distance").getValue();
double charge = Distance*100.0;
deliCha.setText(String.valueOf(charge));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
this the error.
Process: com.example.ketomate, PID: 13156
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at com.example.ketomate.BillDetailsForPaymentAndDelivery$2.onDataChange(BillDetailsForPaymentAndDelivery.java:121)
at com.google.firebase.database.Query$1.onDataChange(Query.java:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案1
得分: 1
你可以使用Double.valueOf
方法
double Distance = Double.valueOf(dataSnapshot.child("Distance").getValue());
英文:
You can use the Double.valueOf
method
double Distance = Double.valueOf (dataSnapshot.child("Distance").getValue());
答案2
得分: 1
"6.016539 Kilometers"字符串无法转换为双精度数,因为它含有"Kilometers"一词。在转换为双精度数之前,您需要先移除"Kilometers"一词。
private void getData(final TextView deliCha) {
rootRef.child(appId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String distance = dataSnapshot.child("Distance").getValue();
String[] separate = distance.split("Kilometers");
double d = Double.parseDouble(separate[0]);
double charge = d * 100.0;
deliCha.setText(String.valueOf(charge));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
英文:
"6.016539 Kilometers" string can not be converted to double because it has the word Kilometers. first you need to remove the word Kilometers before you convert to double.
private void getData(final TextView deliCha) {
rootRef.child(appId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String distance = dataSnapshot.child("Distance").getValue();
String[] separate = distance.split("Kilometers");
double d = Double.parseDouble(separate[0]);
double charge = d*100.0;
deliCha.setText(String.valueOf(charge));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案3
得分: 0
看不到你的数据库内容,我能得出的结论只有一个,那就是名为"Distance"的子项的值是一个字符串,而不是一个双精度浮点数(Double)。你不能简单地将一个字符串转换为双精度浮点数。
首先要做的是修正向数据库写入值的方式。不要直接写入字符串值,而是先将它们转换为双精度浮点数,这样Firestore在返回时会将它们作为双精度浮点数返回给你。
如果你无法这样做,那么在读取值时就必须将字符串转换为双精度浮点数。
String distance = dataSnapshot.child("Distance").getValue(String.class);
double doubleDistance = Double.parseDouble(distance);
double charge = doubleDistance * 100.0;
deliCha.setText(Double.toString(charge));
英文:
Without seeing the contents of your database, all I can conclude is that the value of a child called "Distance" is a string value, not a Double. You can't just cast a string to a double.
The first thing to do would be to fix the way you write values into the database. Instead of writing String values, convert them to doubles first, so that Firestore will return them to you as a double.
If you can't do that, you will have to convert the String to a double when you read it.
String distance = dataSnapshot.child("Distance").getValue(String.class);
double doubleDistance = Double.parseDouble(Distance);
double charge = doubleDistance * 100.0;
deliCha.setText(Double.toString(charge));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论