[英]Getting the value of a child of a unique ID from Firebase
I'm trying to display the user's current location from the Realtime Database, but it's not displaying on the map, the Database is structured like this:我试图从实时数据库中显示用户的当前位置,但它没有显示在地图上,数据库的结构如下:
{ "Blocked Users" : { "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : 0 }, "User Location" : { "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : { "latitude" : 37.4035483, "longitude" : -122.097715 } }, "Users" : { "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : { "Alert Level" : "High", "Emergency Type" : "Natural Disaster", "address" : "Lapaz Tarlac", "emergencyNum" : "0981232387346", "name" : "Rafael Campos", "phoneNum" : "0981233445675" } } }
I'm getting NPEs when fetching the location from Firebase using this code in onMapReady, how should I fix this?使用 onMapReady 中的此代码从 Firebase 获取位置时,我收到了 NPE,我应该如何解决这个问题?
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Users");
DatabaseReference uidRef = rootRef.child("User Location");
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
Double latitude = snapshot.child("latitude").getValue(Double.class);
Double longitude = snapshot.child("longitude").getValue(Double.class);
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title(getCompleteAddress(latitude, longitude)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,14F));
Log.d("TAG", "latitude, longitude");
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
}
This is the stack trace:这是堆栈跟踪:
2021-10-23 11:19:19.139 12376-12376/com.example.rescuealertadmin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rescuealertadmin, PID: 12376
java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
at com.example.rescuealertadmin.RetrieveMapsActivity$4.onComplete(RetrieveMapsActivity.java:202)
at com.google.android.gms.tasks.zzi.run(com.google.android.gms:play-services-tasks@@17.2.1:1)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Your User Location
path has a list of users, so your onComplete
needs to handle the fact that the DataSnapshot
has a list of child nodes too by looping over the results:您的User Location
路径有一个用户列表,因此您的onComplete
需要通过循环结果来处理DataSnapshot
也有一个子节点列表的事实:
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
for (DataSnapshot child: snapshot.getChildren()) {
Double latitude = child.child("latitude").getValue(Double.class);
Double longitude = child.child("longitude").getValue(Double.class);
LatLng location = new LatLng(latitude, longitude);
...
}
});
You are finding data on the wrong node.您正在错误的节点上查找数据。
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Users");
Here you have saved Users Information在这里您保存了用户信息
DatabaseReference uidRef = rootRef.child("User Location");
And then you're going into "Users" Node Further.然后您将进一步进入“用户”节点。 But there is NO "User Location" In "Users" Node.但是“用户”节点中没有“用户位置”。
You can get data like this if you have UID如果你有 UID,你可以得到这样的数据
DatabaseReference uidRef = FirebaseDatabase.getInstance().getReference("User Location").child(UID);
You are getting the foll9ow error:您收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double java.lang.Double.doubleValue()”
Because you are using an incorrect reference in your code.因为您在代码中使用了不正确的引用。 As I see, both "latitude" and "longitude" are direct children of the UID.正如我所见,“纬度”和“经度”都是 UID 的直接子代。 So please change the following lines of code:因此,请更改以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Users");
DatabaseReference uidRef = rootRef.child("User Location");
uidRef.get().addOnCompleteListener(/* ... /*);
To:到:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("User Location").child(uid);
uidRef.get().addOnCompleteListener(/* ... /*);
The rest of the code may remain unchanged.其余代码可能保持不变。
You are providing wrong ref path that's why you are getting NPE.您提供了错误的参考路径,这就是您获得 NPE 的原因。
Convert it from:将其转换为:
DatabaseReference rootRef =
FirebaseDatabase.getInstance().getReference("Users");
DatabaseReference uidRef = rootRef.child("User Location");
uidRef.get().addOnCompleteListener(/* ... /*);
to到
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef =
FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("User Location").child(uid);
uidRef.get().addOnCompleteListener(/* ... /*);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.