我做了一个在谷歌地图上显示当前位置的活动,它不起作用,我似乎无法找出原因?

问题描述

public class GrayMapsActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
public Location currentlocation;
public String s1,s2;
public Double d1,d2;
TextView txt1,txt2;
private FusedLocationProviderClient fusedLocationClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    d1=0.0;
    d2=0.0;
    super.onCreate(savedInstanceState);
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    setContentView(R.layout.activity_gray_maps_activity);
    SupportMapFragment mapFragment = (SupportMapFragment) 
    getSupportFragmentManager().findFragmentById(R.id.map);
    assert mapFragment != null;
    mapFragment.getMapAsync(this);
    txt1 = (TextView) (findViewById(R.id.textView1));
    txt2 = (TextView) (findViewById(R.id.textView2));
    if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != 
    PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    fusedLocationClient.getLastLocation()
            .addOnSuccessListener(this,new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last kNown location. In some rare situations this can be null.
                    if (location != null) {
                        d1= location.getLatitude();
                        d2= location.getLongitude();
                    }
                }
            });

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
   LatLng graycarlocation= new LatLng(d1,d2);
            mMap.addMarker(new MarkerOptions()
                   .position(graycarlocation)
                    .title("Gray Car Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(graycarlocation));

  }


  }

“这是主要活动,我使用融合位置来获取最后一个已知位置,然后将其插入到双变量中的经度和纬度,由于某种原因,当地图开始时它不会到达该位置,

出现的错误代码: 尝试在空对象引用上调用虚拟方法“double java.lang.Double.doubleValue()” "

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...