本文介绍了Android-尝试在空对象引用上调用虚拟方法'double java.lang.Double.doubleValue()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有两个帐户,家庭帐户和老人帐户.我想将老年人的位置检索到家庭账户,然后在地图上显示出来.现在,我想获得纬度和经度.我已经成对设置了帐户.

There are two accounts in my app, family and elderly. And I want to retrieve the location of the elderly to the family account, then show it in a map. Now, I want to get the latitude and longitude. I have set the accounts in a pair.

这是数据库结构.

更新:

更新的代码:

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

The error happens in getting the latitude in getCurrentLocation function

推荐答案

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

To fix this, please use the following code:

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是基本类型而不是类 .正确的方法是使用 Double 类.

To get the data, you need to iterate over the DataSnapshot object and not on the DatabaseReference object. And you cannot use getValue(double.class). double is a primitive not a class. The correct way is use Double class.

这篇关于Android-尝试在空对象引用上调用虚拟方法'double java.lang.Double.doubleValue()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:46