我正在尝试保存并从Firebase
检索位置并将其放入Google地图中。纬度和经度值在用户ID下,因为它将用于特定用户的位置。保存位置是成功的,但我无法检索它。我正在尝试检索它,但我一直在
java.lang.NullPointerException:尝试调用虚方法 对空对象引用'double java.lang.Double.doubleValue()' 错误。
有什么方法可以解决或做到这一点?提前谢谢。
以下是保存位置的代码:
profDatabase = FirebaseDatabase.getInstance().getReference().child("Profile");
if (auth.getCurrentUser() != null){
String userid = auth.getCurrentUser().getUid();
final DatabaseReference locDatabase = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://sholoco-9cf1e.firebaseio.com/Profile/" + userid);
profDatabase.child(userid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String stallname = dataSnapshot.child("StallName").getValue().toString();
Marker marker = mMap.addMarker(new
MarkerOptions().position(point).title(stallname));
final LatLng latlng = marker.getPosition();
mSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//DatabaseReference newPost = profDatabase.push();
//newPost.setValue(latlng);
String latkey = "latitude";
double latvalue = latlng.latitude;
DatabaseReference child = locDatabase.child(latkey);
child.setValue(latvalue);
String longkey = "longitude";
double longvalue = latlng.longitude;
child = locDatabase.child(longkey);
child.setValue(longvalue);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我检索位置的代码:
profDatabase = FirebaseDatabase.getInstance().getReference().child("Profile");
if (auth.getCurrentUser() != null) {
String userid = auth.getCurrentUser().getUid();
profDatabase.child(userid).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
double latt = (double) dataSnapshot.child("latitude").getValue();
double longg = (double) dataSnapshot.child("longitude").getValue();
String stallname = dataSnapshot.child("StallName").getValue().toString();
LatLng newLocation = new LatLng(latt, longg);
mMap.addMarker(new MarkerOptions()
.position(newLocation)
.title(stallname));
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我的数据库中的截图:
答案 0 :(得分:2)
您是否尝试过使用addListenerForSingleValueEvent()
代替addChildEventListener()
?
获取没有userid
的值:
请参阅https://stackoverflow.com/a/46651960/4653447
FirebaseDatabase.getInstance().getReference().child("Profile").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
double latt = (double) snapshot.child("latitude").getValue();
double longg = (double) snapshot.child("longitude").getValue();
//store this data in list
}
}
答案 1 :(得分:1)
您不需要将投射纬度和经度输入double
,而是将Double.class
作为参数传递给getValue()
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
double latt = dataSnapshot.child("latitude").getValue(Double.class);
double longg = dataSnapshot.child("longitude").getValue(Double.class);
String stallname = dataSnapshot.child("StallName").getValue().toString();
LatLng newLocation = new LatLng(latt, longg);
mMap.addMarker(new MarkerOptions()
.position(newLocation)
.title(stallname));
}
答案 2 :(得分:0)
此代码有效
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationsRef = rootRef.child("childName");
final GeoFire geoFire = new GeoFire(locationsRef);
geoFire.getLocation("dataKey", new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
double lat =location.latitude;
double lng =location.longitude;
LatLng sydney = new LatLng(lng, lat);
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(HUE_BLUE)).position(sydney).title("car"));
Toast.makeText(getApplicationContext(),""+sydney,Toast.LENGTH_SHORT).show();
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.zoomTo(9));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap = googleMap;
mMap.setMyLocationEnabled(true);
buildGoogleApiClient();
}