maidi
maidi

Reputation: 3459

Google Maps returns null

I have a Google Maps Activity. suddently it is shutting down because getMap() (java null pointer exeption) doesn't seem to work. I reinstalled some older apks that worked fine and but yet these are shutting down to. So it has to be something on the device or so, what can be the reason?

private GoogleMap googleMap;   
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
try {
        if(googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        }

Here is the xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"/>

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.mareike.cityexplorer/de.mareike.cityexplorer.MainActivity}: java.lang.NullPointerException
 Caused by: java.lang.NullPointerException
        at com.google.l.a.ce.a(Unknown Source)
        at com.google.maps.api.android.lib6.d.eg.<init>(Unknown Source)
        at com.google.maps.api.android.lib6.d.ef.a(Unknown Source)
        at com.google.maps.api.android.lib6.d.aq.<init>(Unknown Source)
        at com.google.maps.api.android.lib6.d.ft.a(Unknown Source)
        at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
        at android.os.Binder.transact(Binder.java:380)
        at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
        at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
        at de.mareike.cityexplorer.MainActivity.onCreate(MainActivity.java:88)

Upvotes: 0

Views: 826

Answers (2)

bjiang
bjiang

Reputation: 6078

According to here, the getMap() method is deprecated and you need to use getMapAsync(OnMapReadyCallback) instead.

Sample code:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 10));

    }
}

and the activity_main.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/map"
          tools:context=".MapsActivity"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

For more details, please refer official doc here.

Upvotes: 1

kartikag01
kartikag01

Reputation: 1579

Check your layout activity_main should contains fragment having id map

 <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Upvotes: 0

Related Questions