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