Dimitri Lozovoy
Dimitri Lozovoy

Reputation: 453

Android Google Maps API v2: FragmentManager.findFragmentById() always returns NULL

While developing an app that involves Android Google Maps API v2.0, I find that FragmentManager.findFragmentById(<resource ID of fragment XML file>) always returns NULL. I inflated the fragment from an XML file. I can get the View no problem, but what about the Fragment itself? I even tried a 4 sec delay to make sure by the time I get the ID everything is loaded.

//
// res/layout/mapfragment.xml:
//

<?xml version="1.0" encoding="UTF-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/map"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              class="com.google.android.gms.maps.SupportMapFragment"/>

// Transmitting resource ID of fragment in XML

    ui.setMapFragmentResourceID(R.layout.mapfragment);

..

// Showing map

    @Override
        public void showMap() {

    ...

        m_map = m_currentActivity.getLayoutInflater().inflate(m_mapFragmentResourceID, null);

        cv.postDelayed(new Runnable() {
            @Override
            public void run() {
                afterShowMapView();
            }
        }, 4000);
    }

// 4000 msec delay to allow for everything to load

    public void afterShowMapView()
    {
        FragmentActivity fa = (FragmentActivity)m_currentActivity;

        FragmentManager fm = fa.getSupportFragmentManager();
        SupportMapFragment f = (SupportMapFragment)fm.findFragmentById(m_mapFragmentResourceID);
    }

//
// f is always null
//

How can I get a hold of the Fragment/SupportFragment object itself (as opposed to the view that I get from inflating the layout)?

Upvotes: 0

Views: 1745

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

You use the correct ID, which is R.id.map based on your XML. If I understand your code correctly, you are using R.layout.mapfragment.

Upvotes: 2

Related Questions