【问题标题】:Error on "Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference" [duplicate]“尝试在空对象引用上调用虚拟方法'double java.lang.Double.doubleValue()'”时出错[重复]
【发布时间】:2017-12-13 12:35:06
【问题描述】:

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

        }
    }

【问题讨论】:

    标签: java android google-maps nullpointerexception


    【解决方案1】:

    您的纬度和经度为 NULL,因为它们只是被声明并且从未赋值。

    例子:

    纬度=28.6139391;

    经度=77.2068325;

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

    【讨论】:

      【解决方案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(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);
      
              }
          }
      

      【讨论】:

        【解决方案3】:

        你从未为

        赋值
        Double latitude;
        Double longitude;
        

        这是你使用它们的地方。

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

        【讨论】:

        • 消除错误并显示地图活动,但它是空白的,没有显示地图的任何功能
        猜你喜欢
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多