Не удалось преобразовать значение типа java.lang.Строка в long

#android #firebase-realtime-database

#Android #firebase-realtime-database

Вопрос:

Я не вижу ошибки в своем коде, если это сработает, может кто-нибудь сказать мне, как это исправить? заранее спасибо 🙂 любая помощь приветствуется

 public class ListPatrimony extends AppCompatActivity {

 
  long money = 1;
  

 public void onDataChange (DataSnapshot dataSnapshot) {
                        if (dataSnapshot.child ("money").getValue(Long.class) >= 7) <- line error
                        { dataSnapshot.child ("money").getRef().setValue((money));
  

  com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
            at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertLong(com.google.firebase:firebase-database@@19.2.1:384)
            at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database@@19.2.1:295)
            at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:214)
            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.gangster.ui.activities.ListPatrimony$2$1.onDataChange(ListPatrimony.java:87)
  

Ответ №1:

Кажется, вы сохранили money String при сохранении. Итак, вы можете изменить:

 if (dataSnapshot.child ("money").getValue(Long.class) >= 7)
  

Для

 if (dataSnapshot.child ("money").getValue().toLong() >= 7)