【问题标题】:FATAL ERROR com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long致命错误 com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 long
【发布时间】:2021-12-31 00:36:28
【问题描述】:

我收到此错误,但找不到原因。我尝试从 Firebase 检索经纬度。这是我的 Java 类代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {

    private GoogleMap mMap;
    FirebaseFirestore db;
    private DatabaseReference mUsers;
    private ChildEventListener mChildEventListener;
    Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // initializing our firebase firestore.
        db = FirebaseFirestore.getInstance();

        // Obtain the SupportMapFragment and get
        // notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        ChildEventListener mChildEventListener;
        mUsers = FirebaseDatabase.getInstance().getReference("Locations");
        mUsers.push().setValue(marker);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        googleMap.setOnMarkerClickListener(this);
        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mUsers.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.exists()) {
                    for (DataSnapshot dataSnapshot:snapshot.getChildren()) {
                        MapAdapter mapAdapter = dataSnapshot.getValue(MapAdapter.class);
                        LatLng location = new LatLng(mapAdapter.latitude, mapAdapter.longitude);
                        mMap.addMarker(new MarkerOptions().position(location)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

                    }
                }
            }

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

            }
        });


    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }
}

这是我的适配器代码:

public class MapAdapter {
     public Long latitude,longitude ;

    public MapAdapter() {

    }

    public Long getLatitude() {
        return latitude;
    }

    public void setLatitude(Long latitude) {
        this.latitude = latitude;
    }

    public Long getLongitude() {
        return longitude;
    }

    public void setLongitude(Long longitude) {
        this.longitude = longitude;
    }
}

enter image description here

E/AndroidRuntime: 致命异常: main 进程:com.example.wss503,PID:18029 com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 long 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertLong(CustomClassMapper.java:385) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(CustomClassMapper.java:296) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:215) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80) 在 com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203) 在 com.example.wss503.MapsActivity$1.onDataChange(MapsActivity.java:62) 在 com.google.firebase.database.Query$1.onDataChange(Query.java:191) 在 com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) 在 com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) 在 com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) 在 android.os.Handler.handleCallback(Handler.java:938) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

【问题讨论】:

  • 在问题中发布完整的错误。 处出错

标签: java android firebase google-maps firebase-realtime-database


【解决方案1】:

您收到以下错误:

致命错误 com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 long

因为您试图将纬度和经度读取为long 值,而在数据库中存储为字符串,这是不正确的。看到双引号了吗?

要解决这个问题,您可以将数据库中的字段类型更改为数字并保留代码原样,或者像这样更改MapAdapter 类:

public class MapAdapter {
    public String latitude, longitude;

    public MapAdapter() {}

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}

您还可以像这样更改代码:

MapAdapter mapAdapter = dataSnapshot.getValue(MapAdapter.class);
long latitude = Long.parseLong(mapAdapter.latitude);
long longitude = Long.parseLong(mapAdapter.longitude);
LatLng location = new LatLng(latitude, longitude);

【讨论】:

    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2021-05-02
    • 2018-04-09
    相关资源
    最近更新 更多