【发布时间】:2017-03-04 05:59:29
【问题描述】:
我正在开发一个简单的 android 应用程序,我需要在其中创建地图和跟踪位置,就像 Uber 一样...我使用 firebase 作为我的数据库来存储用户的位置。 为了存储位置,我创建了一个在调用 MainActivity 时调用的服务 我面临的问题是该位置仅在数据库中更新一次,即当位置更改时,它不会在数据库中更新,因为我必须重新启动应用程序才能更新新位置。
这是我的位置服务中 onLocationChanged 方法的代码
public void onLocationChanged(Location location) {
Log.d("lam", "onLocationChanged:lam "+location.getLatitude()); //The log gets updated but not the values in database
Map<String,Object> taskMap=new HashMap<>();
taskMap.put("Latitude",location.getLatitude());
taskMap.put("Longitude",location.getLongitude());
try{
urlCurrenUser.updateChildren(taskMap);//urlCurrentUser is the path to database
}catch(Exception e){}
}
我也试过了
public void onLocationChanged(Location location) {
Log.d("lam", "onLocationChanged:lam "+location.getLatitude()); //The log gets updated but not the values in database
try {
urlCurrenUser.child("Lattitude").setValue(location.getLattitude());
urlCurrenUser.child("Longitude").setValue(location.getLongitude());
}catch (Exception e){}
我在主要活动的 onCreate() 中调用了该服务
编辑 我认为我的 urlCurrentUser (对数据库的引用)中存在问题,当应用程序第一次尝试启动时,位置在数据库中得到更新,但是当位置发生变化并再次调用代码来更新新坐标时,它给出了一个异常,即
java.lang.NullPointerException:尝试调用虚拟方法 'com.firebase.client.Firebase com.firebase.client.Firebase.child(java.lang.String)' 在空对象上 参考
我只是不明白如何解决这个问题 这是我的数据库结构的快照 enter image description here
数据库参考代码
urlCurrenUser = new Firebase(URL).child("users").child(authData.getUid());
【问题讨论】: