2013-10-23 27 views
20

initialisiert Ich bin immer einige Berichte zurück von User Experience durch crashlytics gibt mir einen FehlerGoogle Maps CameraUpdateFactory nicht

Fatal Exception java.lang.NullPointerException   
CameraUpdateFactory is not initialized 

Dies ist kein regulärer Absturz, wie in es nicht für jeden Benutzer auftritt, aber es wird zu regelmäßig und ich muss es lösen.

Ich hatte gelesen, dass dies, wenn die Karten nicht initialisiert worden war geschehen könnte, die ich glaube, ich

if(googleMap!=null){ 
       googleMap.animateCamera(CameraUpdateFactory.newLatLng(selectedLatLng)); 
      } 

ein wahrscheinlich behandelt haben verursachen könnte auch sein, dass Google Play-Dienste ist nicht auf dem Gerät oder ist veraltet und ich habe auch eine Bestätigung dafür hinzugefügt.

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(FuelsFragment.this.getActivity()); 

    // Showing status 
    //CAMERAUPDATE FACTORY CRASH CAN BE A RESULT OF GOOGLE PLAY SERVICES NOT INSTALLED OR OUT OF DATE 
    //ADDITIONAL VERIFICATION ADDED TO PREVENT FURTHER CRASHES 

    //https://github.com/imhotep/MapKit/pull/17 
    if(status == ConnectionResult.SUCCESS) 
    { 
     mMapFragment = ReepMapFragment.newInstance(); 

     FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); 
     fragmentTransaction.add(R.id.mapContainer, mMapFragment); 
     fragmentTransaction.commit(); 

    } 
    else if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){ 

     reep.toastNotify("You need to update Google Play Services in order to view maps"); 
    } 
    else if (status==ConnectionResult.SERVICE_MISSING){ 
     reep.toastNotify("Google Play service is not enabled on this device."); 
    } 

Danach bin ich unsicher was als nächstes zu tun, da dies nicht für jeden Benutzer passiert.

Wenn jemand irgendwelche Gedanken darüber, warum dies der Fall ich Ihre Eingabe

schätzen würde

Antwort

0

Mai werden Sie eine Class.forName() tun sollte? für CameraUpdateFactory? in try - catch

21

Ich bekam den gleichen Fehler, obwohl ich ein GoogleMap-Objekt nicht Null von einem MapView erhielt. Ich habe es mit einem expliziten Aufruf von MapsInitializer gelöst, obwohl die Dokumentation besagt, dass es nicht benötigt wird.

Meine App-Fragment stellt die MapView über das Follow-up:

@Override public View 
onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.map_panel, container, false); 
    mapView = (MapView) view.findViewById(R.id.map_view); 
    mapView.onCreate(savedInstanceState); 
    configureMap(mapView.getMap()); 
    return view; 
} 

private void 
configureMap(GoogleMap map, double lat, double lon) 
{ 
    if (map == null) 
     return; // Google Maps not available 
    try { 
     MapsInitializer.initialize(getActivity()); 
    } 
    catch (GooglePlayServicesNotAvailableException e) { 
     Log.e(LOG_TAG, "Have GoogleMap but then error", e); 
     return; 
    } 
    map.setMyLocationEnabled(true); 
    LatLng latLng = new LatLng(lat, lon); 
    CameraUpdate camera = CameraUpdateFactory.newLatLng(latLng); 
    map.animateCamera(camera); 
} 

Bevor ich den Anruf zu MapsInitializer hinzugefügt, würde ich eine Ausnahme von CameraUpdateFactory bekommen. Nach dem Hinzufügen des Anrufs ist CameraUpdateFactory immer erfolgreich.

1

einige Zeit, es durch alte Version von Google Play-Dienst verursacht werden können, bitte Google-Dienst aktualisieren spielen kann es Sie

+0

ich das schon in meiner Frage behandelt haben ... –

11

Nur MapsInitializer.initialize verwenden hilft ohne try-catch, weil es nicht GooglePlayServicesNotAvailableException wirft in der neuesten Version von Google Play-Dienste, die mit Paketversion 5084032.

0

Etwas von meinem MapUtil, dass dieser Fall behandelt wurde verifiziert:

public static void animateCamera(final MapView mapView, final GoogleMap map, final LatLng location) { 
     try{ 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, getBetterZoom(map)); 
      map.animateCamera(cameraUpdate); 
     }catch(Exception e) { 
      MapsInitializer.initialize(mapView.getContext()); 
      if (mapView.getViewTreeObserver().isAlive()) { 
       mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @SuppressLint("NewApi") 
        @Override 
        public void onGlobalLayout() { 
         GlobalyLayoutListenerUtil.removeGlobalLayoutListener(mapView, this); 
         animateCamera(mapView, mapView.getMap(), location); 
        } 
       }); 
      } else { 
       if(map != null){ 
        map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() { 
         @Override 
         public void onMapLoaded() { 
          if(location != null){ 
           animateCamera(mapView, mapView.getMap(), location); 
          } 
         } 
        }); 
       } 
      } 
     } 

    } 
+0

Der obige Code kann aufgrund fehlender GlobalyLayoutListenerUtil & getBetterZoom nicht vollständig verwendet werden - bitte schreiben Sie den fehlenden Code. Vielen Dank. – AlexVPerl