当前位置: 首页 > 知识库问答 >
问题:

Android-尝试对空对象引用调用虚拟方法'double java.lang.Double.DoubleValue()'

湛钊
2023-03-14

我的app里有两个账号,家人和老人。我想把老人的位置检索到家庭账户,然后在地图上显示出来。现在,我要得到纬度和经度。我已把帐目对成一对。

下面是数据库结构。

更新:

更新的代码:

private Button logout;
private ImageButton buttonCurrent;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseUser user;
private double longitude;
private double latitude;
private String currentUserId;
private com.google.firebase.database.Query mQueryMF;


//Our Map
private GoogleMap mMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    currentUserId = user.getUid();
    mDatabase = FirebaseDatabase.getInstance().getReference();


    setContentView(R.layout.activity_main__family);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    logout = (Button) findViewById(R.id.logout);
    buttonCurrent = (ImageButton) findViewById(R.id.buttonCurrent);


    buttonCurrent.setOnClickListener(this);

    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(MainActivity_Family.this, HomeActivity.class));
        }
    });


}
private void getCurrentLocation() {
   mQueryMF = mDatabase.child("users").child("familyId").equalTo(currentUserId);

    mQueryMF.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            latitude = 
dataSnapshot.child("latitude").getValue(Double.class);  
            longitude = 
dataSnapshot.child("longitude").getValue(Double.class);
            //String to display current latitude and longitude
            String msg = latitude + ", "+longitude;

            //Creating a LatLng Object to store Coordinates
            LatLng latLng = new LatLng(latitude, longitude);
            //Adding marker to map
            mMap.addMarker(new MarkerOptions()
                    .position(latLng) //setting position
                    .draggable(true) //Making the marker draggable
                    .title("Current Location")); //Adding a title

            //Moving the camera
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            //Animating the camera
            mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

}

//Function to move the map
/*private void moveMap() {




    //Displaying current coordinates in toast
    //Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}*/

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng latLng = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);
}

@Override
public void onConnected(Bundle bundle) {
    getCurrentLocation();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onMapLongClick(LatLng latLng) {
    //Clearing all the markers
    mMap.clear();

    //Adding a new marker to the current pressed position
    mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .draggable(true));
}

@Override
public void onMarkerDragStart(Marker marker) {

}

@Override
public void onMarkerDrag(Marker marker) {

}

@Override
public void onMarkerDragEnd(Marker marker) {
    //Getting the coordinates
    latitude = marker.getPosition().latitude;
    longitude = marker.getPosition().longitude;
}

@Override
public void onClick(View v) {
    if(v == buttonCurrent){
        getCurrentLocation();
    }
}

在getCurrentLocation函数中获取纬度时出错

共有1个答案

慕高阳
2023-03-14

您可以使用Firebase SDK提供的OrderByChild()方法,尝试以下代码而不是您的代码:

mQueryMF = mDatabase.child("users");
mQueryMF.orderByChild("familyId").equalTo(currentUserId)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                    // The snapshot contains the latitude and the longitude
                    latitude = dataSnapshot.getChild("latitude").getValue(double.class);
                    longitude = dataSnapshot.getChild("longitude").getValue(double.class);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
 类似资料: