无法从Firebase数据库快照读取对象 [英] Unable to read objects from firebase database snapshot

查看:60
本文介绍了无法从Firebase数据库快照读取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firebase读取纬度和经度,然后在这些位置上放置标记.但是似乎有一些错误使应用崩溃,即使在Logcat上我也找不到.尝试在每个语句后放置一些检查日志,并发现问题出在我试图将值放在数据库快照的类对象中的地方.我正在尝试使用循环从快照中获取所有对象.这是我的数据库的结构.

I am trying to read latitude and longitude from Firebase and putting marker on those locations. But there seems some error crashing the app which I am unable to find even on Logcat. Tried putting some check Logs after each statement and found out that the problem occurs where I am trying to put values in class objects from the database snapshot. I am trying to use loop to fetch all objects from snapshot. Here is the structure of my database.

我的Maps活动代码,我试图将数据库快照中的值放到我的类对象中.

My Maps activity code where I am trying to put values from database snapshot to my class objects.

//reading stations from database
List<Stations> stations;
 mDatabase = FirebaseDatabase.getInstance().getReference().child("STATIONS");

    ValueEventListener stationListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //found out that error is here
            for(DataSnapshot stationsSnapshot: dataSnapshot.getChildren()){
                stations.add(stationsSnapshot.getValue(Station.class));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("Station Read[ERROR]",databaseError.getMessage());
        }
    };
    mDatabase.addListenerForSingleValueEvent(stationListener);

我的工作站类代码代码,用于标记从firebase获取的地图上的位置.

Code to mark locations on the map fetched from firebase.

    //Placing all station data on Maps
    for(int i=0 ; i<stations.size() ; i++){
        LatLng location = new LatLng(stations.get(i).getLocation().getLatitude(),
                stations.get(i).getLocation().getLongitude());
        mMap.addMarker(new MarkerOptions().position(location).title(stations.get(i).getName()));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
    }

推荐答案

初始化电台列表.

List<Stations> stations= new ArrayList<>();

添加标记的逻辑对我来说似乎不错.正如Momen所说,只需将其添加到正确的位置即可.

The logic of adding the markers seems alright to me. Just adding it at the right place, as told by Momen, should work.

这篇关于无法从Firebase数据库快照读取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆