Step Add
Step Add

Reputation: 11

GoogleMap complains of being a null object reference

I am working on an android application where I use maps and this is the error that I am getting

Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMapType(int)' on a null object reference

Prior to posting my query here I have looked for other similar issues but my code already have all what is being suggested so I was hoping if someone could point out the error.

This is the line which throws the error(second line). I have no idea why it is caling map null when I've assigned it (at least I think I have)

private void initViews() {
    map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
    //map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}

and also this is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="65dp" >

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

</LinearLayout>

Upvotes: 0

Views: 210

Answers (1)

fweigl
fweigl

Reputation: 22008

Use getMapAsync(...) instead of getMap(). Example can be found here.

Upvotes: 1

Related Questions