[英]java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
I want to multiply a value after retrieving it from the database(realtime firebase) and assign to another variable.我想在从数据库(realtime firebase)中检索一个值并将其分配给另一个变量后乘以一个值。 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)
You can use the Double.valueOf
method您可以使用Double.valueOf
方法
double Distance = Double.valueOf (dataSnapshot.child("Distance").getValue());
"6.016539 Kilometers" string can not be converted to double because it has the word Kilometers. “6.016539 Kilometers”字符串无法转换为 double,因为它包含 Kilometers 一词。 first you need to remove the word Kilometers before you convert to double.首先,您需要在转换为 double 之前删除 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) {
}
});
}
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.在没有看到您的数据库内容的情况下,我只能得出结论,名为“Distance”的子项的值是一个字符串值,而不是 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.不要写入字符串值,而是先将它们转换为双精度值,以便 Firestore 将它们作为双精度值返回给您。
If you can't do that, you will have to convert the String to a double when you read it.如果您不能这样做,则在读取时必须将 String 转换为 double。
String distance = dataSnapshot.child("Distance").getValue(String.class);
double doubleDistance = Double.parseDouble(Distance);
double charge = doubleDistance * 100.0;
deliCha.setText(Double.toString(charge));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.