错误"尝试调用虚方法' double java.lang.Double.doubleValue()'在null对象引用"

时间:2017-12-13 12:35:07

标签: java android google-maps nullpointerexception

我刚从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);

        }
    }

3 个答案:

答案 0 :(得分:2)

尝试将值分配给纬度和经度 - :

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(本);             }

        @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);

        }
    }

答案 1 :(得分:2)

您的纬度和经度为NULL,因为它们仅被声明且值从未分配。

示例:

纬度= 28.6139391;

经度= 77.2068325;

设置这样的位置,这个位置是德里

答案 2 :(得分:1)

您从未将值分配给

Double latitude;
Double longitude;

这是您使用它们的地方。

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