"Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object referen



我刚刚通过访问SqliteDatabase的纬度和经度来显示地图的问题。

当我运行此操作时,我会在线路.target(new LatLng(latitude, longitude))上获得上述错误。任何帮助,将不胜感激。

 import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.AppCompatActivity;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import static com.example.kod45.refapp.R.id.lat;
    import static com.example.kod45.refapp.R.id.longitude;
    public class Map extends FragmentActivity implements OnMapReadyCallback {
        Double latitude;
        Double longitude;
        private GoogleMap mMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            // Add a marker in Tallaght and move the camera
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude))      // Sets the center of the map to Mountain View
                    .zoom(17)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            Intent intent = getIntent();
            intent.putExtra("latitude", latitude);
            intent.putExtra("longitude", longitude);
    //        LatLng sydney = new LatLng(latitude, longitude);
        }
    }

您的纬度和经度是零的,因为它们仅声明并且从未分配了价值。

示例:

纬度= 28.6139391;

经度= 77.2068325;

设置这样的位置,此位置为德里

尝试将值分配给纬度和长度 -

import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.AppCompatActivity;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import static com.example.kod45.refapp.R.id.lat;
    import static com.example.kod45.refapp.R.id.longitude;
    public class Map extends FragmentActivity implements OnMapReadyCallback {
        Double latitude;
        Double longitude;
        private GoogleMap mMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);

将价值添加到纬度和长度的价值"纬度" ="长距离" = mapFragment.getMapAsync(this); }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            // Add a marker in Tallaght and move the camera
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude))  //here latitude and longtitude are null as no value is assigned to them    // Sets the center of the map to Mountain View
                    .zoom(17)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            Intent intent = getIntent();
            intent.putExtra("latitude", latitude);  
            intent.putExtra("longitude", longitude);
    //        LatLng sydney = new LatLng(latitude, longitude);
        }
    }

您从未将值分配给

Double latitude;
Double longitude;

这是您使用它们的地方。

 .target(new LatLng(latitude, longitude))      // Sets the center of the map to Mountain View

最新更新