2014-06-27 9 views
10

Ich arbeite mit Google Maps. Ich kann erfolgreich erstellen und kann eine Google Map zeigen. Jetzt möchte ich CameraUpdate(latitude, longitude) hinzufügen. Ich googelte und ich fand Quellcode, aber ich bekomme eine NullPointerException.java.lang.NullPointerException: CameraUpdateFactory ist nicht initialisiert logcat Ausnahme

Die LogCat Fehlermeldung lautet:

java.lang.NullPointerException: CameraUpdateFactory is not initialized 

Diese meine Quelle ist. Was mache ich falsch?

public class StradaContact extends Fragment { 

public final static String TAG = StradaContact.class.getSimpleName(); 
private MapView mMapView; 
private GoogleMap mMap; 
private Bundle mBundle; 
double longitude = 44.79299800000001, latitude = 41.709981; 

public StradaContact() { 

} 

public static StradaContact newInstance() { 
    return new StradaContact(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.strada_contact, container,false); 

    mMapView = (MapView) rootView.findViewById(R.id.pointMap); 

    mMapView.onCreate(mBundle); 
    setUpMapIfNeeded(rootView); 

    return rootView; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mBundle = savedInstanceState; 
} 

private void setUpMapIfNeeded(View inflatedView) { 
    if (mMap == null) { 
     mMap = ((MapView) inflatedView.findViewById(R.id.pointMap)).getMap(); 
     if (mMap != null) { 
      CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)); 
      CameraUpdate zoom = CameraUpdateFactory.zoomTo(15); 
      mMap.moveCamera(center); 
      mMap.animateCamera(zoom); 
     } 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    mMapView.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mMapView.onPause(); 
} 

@Override 
public void onDestroy() { 
    mMapView.onDestroy(); 
    super.onDestroy(); 
} 

}

<com.google.android.gms.maps.MapView 
     android:id="@+id/pointMap" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     class="com.google.android.gms.maps.MapFragment" /> 
+0

es überprüfen: http: //stackoverflow.com/questions/19541915/google-maps-cameraupdatefactor y-nicht-initialisiert – Ranjit

Antwort

6

Versuchen Sie es mit Methode folgende initialisieren api google map vor

MapsInitializer.initialize(getActivity()); 
+0

Warum ist das besser als 'getMap()'? –

19

Versuchen Sie dies in "onCreate" mit:

try { 
MapsInitializer.initialize(this); 
} catch (GooglePlayServicesNotAvailableException e) { 
e.printStackTrace(); 
} 
+0

es funktionierte für mich =) –

+0

Ich rufe bereits die 'getMap()' Methode von SupportMapFragment auf. Warum brauche ich auch MapsInitializer? –

Verwandte Themen