0

I'm trying to load a google map in a fragment. The problem is that when I make the call to get the GoogleMap object, the call to findFragmentById is always coming back null. I've looked at every post out there and tried almost all of them and can't figure this out.

My app structure is like so: 1. I have an Activity (MINMainActivity) that extends ActionBarActivity. 2. In onCreate I call the following:

 mMainView = getLayoutInflater().inflate(R.layout.mimaster_drawer_layout, null);

The source for mimaster_drawer_layout is:

 <?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/init_background">
</FrameLayout>
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/black"
    android:dividerHeight="1dp"
    android:background="@android:color/darker_gray"/>

I switch out the contents of the FrameLayout (content_frame) with a dynamic list of Fragments. One of those fragments contains a SupportMapFragment. The class definition is:

 public class MINPageTypeMappingFragment extends Fragment

The onCreateView code is:

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    // Inflate view
    //pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false);
    pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false);

    // Set up specific controls/views
    SetupMapIfNeeded();

    return pageView;
}

The code for SetupMapIfNeeded is:

     private void setUpMapIfNeeded() 
{
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null)
    {
        // Try to obtain the map from the SupportMapFragment.
        SupportMapFragment mapfrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        if(mapfrag != null)
        {
            mMap = mapfrag.getMap();
        }

        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

page_mapping_fragment.xml is:

 <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=".MINMainActivity" >

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

EDIT: I know that the class should equal class="com.google.android.gms.maps.SupportMapFragment" but when I do that, it crashes during the initial expansion in onCreateView.

I've also included the relevant portions of the manifest file:

 <?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
    android:name="com.mobilityinitiative.ironman.MINApplication">

    <!-- Mobility Initiative Activities -->
    <activity android:name=".MINMainActivity"
              android:configChanges="orientation|screenSize|keyboardHidden"
              android:label="@string/title_minpagedefinition_list" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

</application>

Like I said, the line that's giving the grief is:

             // Try to obtain the map from the SupportMapFragment.
        SupportMapFragment mapfrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

mapFrag is always null. I'm something of a noob and so forgive me my ignorance. I also got a list of fragments using the following line:

 List<Fragment> frags = getActivity().getSupportFragmentManager().getFragments();

This did not come back as expected. I got 3 fragments in the list: 1) MINPageTypeMappingFragment 2) RetainFragment - a fragment from an ImageCache - not relevant 3) MINPageTypeGridFragment - the previous fragment that was replaced with MINPageTypeMappingFragment

I don't see a "SupportMapFragment" like what I expected.

Most of the example code I could find entailed loading a SupportMapFragment from a FragmentActivity. In my case, the fragment that contains the GoogleMap is loaded from MINMainActivity like so (based on button event):

             pageFragment = new MINPageTypeMappingFragment();
        pageFragment.setArguments(arguments);

        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        ft.replace(R.id.content_frame, pageFragment);
        ft.commit();

HELP!!!!!

EDIT:

I know that the class should equal class="com.google.android.gms.maps.SupportMapFragment" but when I do that, it crashes during the initial expansion in onCreateView on the line: pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false); The error I get is: "InflateException: Binary XML file line 7: Error inflating class fragment."

JustLearningAgain
  • 2,227
  • 4
  • 34
  • 50
  • Hello! I faced with a same problem. If you use mapFragment inside another fragment, try to use getChildFragmentManager() instead getSupportFragmentManager(). – Vlad Hudnitsky Dec 03 '14 at 08:35

3 Answers3

1

You are using the standard map fragment class so try this if you have FragmentManager and not supportFragmentManager

private GoogleMap googleMap;

googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap(); 
sunil
  • 660
  • 8
  • 20
  • This worked!!! But here's my question. Why can't I use SupportMapFragment? If I change the class def in the XML file to SupportMapFragment, it crashes (exception) in onCreateView during the inflate call. The exception is: "InflateException: Binary XML file line 7: Error inflating class fragment." I think I need to use SupportMapFragment OR do I? I need backward compatibility. – JustLearningAgain Aug 20 '14 at 15:45
  • if you change the class def in the XML file to SupportMapFragment then use getSupportFragmentManager to find the fragment and rest is explain by @Dilavar – sunil Aug 20 '14 at 15:54
  • I understand that. The problem is that I get an exception when I attempt to inflate the XML: pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false); Since I get that exception, I never get to the code to call getSupportFragmentManager. For some reason, the inflate call doesn't like the class being equal to class="com.google.android.gms.maps.SupportMapFragment". If the line is: class="com.google.android.gms.maps.MapFragment", I don't get the exception. And now I'm back to where I started from. – JustLearningAgain Aug 20 '14 at 16:35
  • where is XML file page_mapping_fragment – sunil Aug 20 '14 at 16:41
  • Sorry. I miss-labeled it above. I just corrected it. I also just implemented the solution posted by Kalab at http://stackoverflow.com/questions/14565460/error-opening-supportmapfragment-for-second-time. This worked but I'm still confused why we can't expand it within the xml. – JustLearningAgain Aug 20 '14 at 17:13
0

You are using the standard map fragment class while trying to fetch the support one.

Replace your fragment class to

class="com.google.android.gms.maps.SupportMapFragment"
Simas
  • 43,548
  • 10
  • 88
  • 116
  • I tried this and now it crashes in onCreateView on the line: pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false); The error I get is: "InflateException: Binary XML file line 7: Error inflating class fragment." – JustLearningAgain Aug 20 '14 at 04:45
0

you use in the XMl file

class="com.google.android.gms.maps.MapFragment"

in java file you used SupportMapFragment Which is support.v4 library use so you get the null pointer exception .

You replace code in your

miMaster_drawer_layout

    <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=".MINMainActivity" >

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
       class="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>
Dilavar Malek
  • 1,157
  • 11
  • 18
  • I added this comment on another answer but just in case, I'm putting it here as well. I tried this and now it crashes in onCreateView on the line: pageView = inflater.inflate(R.layout.page_mapping_fragment, container, false); The error I get is: "InflateException: Binary XML file line 7: Error inflating class fragment." – JustLearningAgain Aug 20 '14 at 15:38