Java getMapAsync()没有回调onMapReady()以初始化googleMap对象
我一直在谷歌地图上工作,下面是我用来初始化地图的方法。令人惊讶的是,mapFragment.getMapAsync()方法没有得到执行,所以googleMaps对象在调用getMapAsync()后仍然为null 以下是Logcat快照: 请注意,getMapAsync中的Log语句永远不会执行。有人能帮我吗 编辑:Java getMapAsync()没有回调onMapReady()以初始化googleMap对象,java,android,google-maps,Java,Android,Google Maps,我一直在谷歌地图上工作,下面是我用来初始化地图的方法。令人惊讶的是,mapFragment.getMapAsync()方法没有得到执行,所以googleMaps对象在调用getMapAsync()后仍然为null 以下是Logcat快照: 请注意,getMapAsync中的Log语句永远不会执行。有人能帮我吗 编辑: 有人将此问题标记为的副本,请注意,此问题是关于getMapAync()如何以及何时调用onMapReady()方法的(与空指针异常无关)您应该将使用变量googleMap的代码移动
有人将此问题标记为的副本,请注意,此问题是关于getMapAync()如何以及何时调用onMapReady()方法的(与空指针异常无关)您应该将使用变量
googleMap
的代码移动到onMapReady(…){…}
方法,因为getMapAsync(…)
是异步的,并且onMapReady(…)
将在Maps响应后在其他线程中调用。可能重复感谢@Selvin的提示。请你详细说明一下好吗。我是androidReason的新手,请投反对票?看起来onMapReady()从未被调用过。请参阅日志语句。它从未显示在输出中。您的代码尝试在Google Maps响应之前访问变量googleMap
,并调用onMapReady
。有关更多信息,请参见教程[1]。所有的映射处理都在onMapReady(…){…}
方法中。[1]
// Initialize the google map
void initMap() {
Log.d(MAPSACTIVITY_TAG, "Initializing google map");
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.maps_fragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap gMap) {
Log.d(MAPSACTIVITY_TAG,"Maps is ready");
googleMap=gMap;
}
});
Log.d(MAPSACTIVITY_TAG,"Value of google maps is "+googleMap);
//This will get device location, set GoogleAPIClient and set mundu marker
if (isLocationPermissionGranted) {
getDeviceLocation();
buildGoogleApiClient();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Sorry! App requires location permission", Toast.LENGTH_SHORT).show();
return;
}
googleMap.setMyLocationEnabled(true);
//this will get client's location and set client's marker
geoLocateClient();
} else
Log.d(MAPSACTIVITY_TAG, "Location permission is not given");
}