Kid*_*PAP 1 android google-maps
在我将 getMap() 更改为 getMapAsync() 并实现 OnMapReadyCallback 后,我收到错误 NullPointerException。
这里是 logcat
引起:java.lang.NullPointerException:尝试在 pym.example.com.fypv2.callme.Callme 的空对象引用上调用虚拟方法“void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)” .onCreate(Callme.java:117)`
这里的代码
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Enabling MyLocation in Google Map
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mGoogleMap.setMyLocationEnabled(true);
Run Code Online (Sandbox Code Playgroud)
和 OnMapReady
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
}
Run Code Online (Sandbox Code Playgroud)
你需要mGoogleMap.setMyLocationEnabled(true);在你的地图准备好后打电话onMapReady。
更改您的代码如下:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mGoogleMap.setMyLocationEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3760 次 |
| 最近记录: |