Android-尝试调用虚方法' double java.lang.Double.doubleValue()'在null对象引用上

时间:2017-12-02 05:08:33

标签: android firebase

我的应用程序中有两个帐户,家庭和老人。我想将老人的位置检索到家庭帐户,然后在地图中显示。现在,我想获得经度和纬度。我已将帐户设置为一对。

这是数据库结构。

更新

更新的代码:

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函数中获取纬度时会发生错误

2 个答案:

答案 0 :(得分:1)

您可以使用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) {
                    }
                });

答案 1 :(得分:1)

要解决此问题,请使用以下代码:

mQueryMF = mDatabase.child("users").child("familyId").equalTo(currentUserId);

mQueryMF.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        double latitude = dataSnapshot.child("latitude").getValue(Double.class);
        double longitude = dataSnapshot.child("longitude").getValue(Double.class);
    }

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

要获取数据,您需要遍历DataSnapshot对象而不是DatabaseReference对象。你不能使用getValue(double.class)。的 double is a primitive not a class 即可。正确的方法是使用Double类。