我的应用程序调用了getmap()方法并注意到它已被删除,而且我已经使用了getMapAsync(),但是我已经阅读了互联网上所有帖子并且没有人帮助我。有时我得到"空指针返回消息"。我希望有人帮助我做到这一点......
我的MapFragment.java:
private void setUpMapIfNeeded() {
if (map == null) {
map = ((MapView) view.findViewById(R.id.map)).getMap();
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.getUiSettings().setZoomControlsEnabled(false);
btnMyLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Location loc = map.getMyLocation();
if (myLocation != null) {
LatLng latLang = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
animateCameraToMarker(latLang, true);
}
}
});
map.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition camPos) {
if (currentZoom == -1) {
currentZoom = camPos.zoom;
} else if (camPos.zoom != currentZoom) {
currentZoom = camPos.zoom;
return;
}
if (!isMapTouched) {
curretLatLng = camPos.target;
if (!isAddDestination) {
// layoutMarker.setVisibility(LinearLayout.VISIBLE);
if (listType.size() > 0) {
stopUpdateProvidersLoaction();
getAllProviders(curretLatLng);
}
getAddressFromLocation(camPos.target, etSource);
}
}
isMapTouched = false;
}
});
}
}
onCreateView: @覆盖 public View onCreateView(LayoutInflater inflater,ViewGroup容器, Bundle savedInstanceState){
... ... setUpMapIfNeeded(); ... 返回视图; }
我的fragment_map.xml
<FrameLayout
android:id="@+id/mapFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="visible" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />