我在super.onResume
中使用Google地图发生了一些随机崩溃。它只发生在调试模式下,而不是生产中。奇怪的是如何将地图添加到片段中(不是由我自己编码),但之前它正常工作。有一天开始崩溃。您可以在下面看到错误和代码。我真的很感激一些帮助!
Fatal Exception: java.lang.NullPointerException
com.google.maps.api.android.lib6.e.y.b (Unknown Source)
com.google.android.gms.maps.MapFragment.onResume (Unknown Source)
com.xxxx.xxxx.Fragments.FragmentMap.onResume (FragmentMap.java:983)
com.xxxx.xxxx.Fragments.FragmentMap.onConnected(FragmentMap.java:2667)
com.google.android.gms.common.internal.zzk.zzk (Unknown Source)
dalvik.system.NativeStart.main (NativeStart.java)
这就是绘制地图的片段:
public class FragmentMap extends MapFragment implements LocationListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<LocationSettingsResult> {
GoogleMap map;
GoogleApiClient
{...}
@Override
public void onCreate(Bundle savedInstanceState) {
//Instantiate here all not view objects (Fragments good practices)
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (checkPlayServices()) {
// Building the GoogleApi client
buildGoogleApiClient();
createLocationRequest();
buildLocationSettingsRequest();
checkLocationSettings();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
map = this.getMap();
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
RelativeLayout rl = new RelativeLayout(getActivity());
if (condition1) {
View firstOverlay = inflater.inflate(R.layout.first_map_layout, null);
rl.addView(mapView);
rl.addView(firstOverlay);
setHasOptionsMenu(true);
} else {
View srcvAddress = inflater.inflate(R.layout.address_finder, null);
rl.addView(mapView);
rl.addView(srcvAddress);
}
return rl;
}
@Override
public void onResume() {
super.onResume(); <----------------------- **HERE CRASHES!!**
stopUpdatePositionService();
if (mGoogleApiClient.isConnected()) {
{...}
}
}
/**
* Creating google api client object
*/
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
/**
* Stopping location updates
*/
private void stopLocationUpdates() {
if (mGoogleApiClient.isConnected())
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
@Override
public void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
/**
* Google api callback methods
*/
@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}
@Override
public void onConnected(Bundle arg0) {
// Once connected with google api, get the location
onResume();
//displayLocation();
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
/**
* Creating location request object
*/
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
提前致谢!
答案 0 :(得分:0)
地图= this.getmap(); ---&GT;可能是问题所在 而是尝试这个
map= ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();//where R.id.map is google map fragment
答案 1 :(得分:0)
注意将来:忘掉getMap。它已被弃用。当我将所有内容移动到新的getMapAsync时,它正常运行