Android GoogleMap or SupportMapFragment - null pointer exception
Asked Answered
C

8

5

In my app i showing google map version2 in a fragment. but i get Null pointer exception at

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

This is my full code:

    public class SearchResultMap extends Fragment{

    private GoogleMap mMap;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.map, container, false);

        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // line no : 28

         mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
         .snippet("xxxxx"));

        return fragmentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

Manifest xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fssd.spot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.fssd.spot.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.fssd.spot.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.fssd.spot.SplashScreen"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:noHistory="true"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.fssd.spot.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/tabTheme" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />

    </application>

</manifest>

whole log cat:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:28)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.Fragment.performStart(Fragment.java:1499)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
Crosscheck answered 4/9, 2013 at 10:57 Comment(1)
Have you download google play service library in your sdk?Celadon
G
10

try to change this:

FragmentManager myFM = getActivity().getSupportFragmentManager();

final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                .findFragmentById(R.id.map);
Graiggrail answered 4/9, 2013 at 11:19 Comment(0)
S
18

None of these solutions fixed it for me -- but this answer from Mark Murphy (https://code.google.com/p/android/issues/detail?id=77714) did:

Use getChildFragmentManager() to access a nested fragment, not getFragmentManager(). http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29

i.e.:

public class MapFragment extends Fragment {
    private GoogleMap map;
    private SupportMapFragment mapFragment;
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
        View view = inflater.inflate(R.layout.map_view, vg, false);
        FragmentManager fm = getChildFragmentManager();
        mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
                initMap();
            }
        });
    }
}
Stylite answered 1/1, 2015 at 16:31 Comment(1)
+1 for showing it inside onCreateView I was having trouble getting it to work because I was doing it inside onCreateSchleswigholstein
G
10

try to change this:

FragmentManager myFM = getActivity().getSupportFragmentManager();

final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                .findFragmentById(R.id.map);
Graiggrail answered 4/9, 2013 at 11:19 Comment(0)
W
0

Try changing to

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

You're using getFragmentManager(), but casting it to SupportMapFragment

You also need to extend FragmentActivity: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

Wimsatt answered 4/9, 2013 at 11:12 Comment(3)
i get exception : The method getSupportFragmentManager() is undefined for the type SearchResultMap.Crosscheck
@murali_ma Is your activity that uses this fragment extending FragmentActivity and not just activity?Wimsatt
thanks. my main activity extends fragment activity and other class extends fragments.Crosscheck
C
0
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

if(map != null) {
         mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
         .snippet("xxxxx"));
}

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />

place your google api key here. You get from google Read the documentation.

Celadon answered 4/9, 2013 at 11:21 Comment(0)
L
0

After hours of search, I did in this way:

 public class MapFragment extends Fragment {
    private GoogleMap map;
    SupportMapFragment mapFrag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.fragment_map, container,
                false);

        fm = getActivity().getSupportFragmentManager();
        map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();

        return rootView;

    }
}
Lashonda answered 27/8, 2014 at 6:29 Comment(0)
M
0

Try to add tools:context=".YourActivity" to the fragment xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".YourActivity" />
Mccants answered 21/5, 2015 at 11:8 Comment(0)
U
0

May be above answers work but what work for me is this.

 {
    SupportPlaceAutocompleteFragment supportPlaceAutocompleteFragmentSearch;
    FragmentManager fragmentManagerSearch = getChildFragmentManager();
    supportPlaceAutocompleteFragmentSearch = 
                                            (SupportPlaceAutocompleteFragment)
                                            fragmentManagerSearch.findFragmentById(R.id.place_autocomplete_fragment_search);
    }
Unorthodox answered 12/5, 2016 at 6:7 Comment(0)
G
0

if you developed your code with Kotlin, and want to add MapFragment to another Fragment you need to use SupportMapFragment but if you do it, you will get null exception so you should use childFragmentManager rather than supportFragmentManager and the code should be in onCreateView not in onCreate just add below code:

private lateinit var mapFragment : SupportMapFragment

 mapFragment = childFragmentManager.findFragmentById(R.id.mapview) as SupportMapFragment
    mapFragment.getMapAsync { googleMap ->
        this.googleMap = googleMap
        setGoogleMapSettings()
    }
Gathering answered 16/10, 2019 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.