0

I have a frame layout in my fragment layout and I can't access map fragment after I replace the frame layout with map fragment. I am having null pointer exception when I try to access the map after I put it into frame layout.

Here's my related layout:

 <FrameLayout
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="15" >
    </FrameLayout>

Here's my code in fragment:

private GoogleMap map;
@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();

        fm.beginTransaction()
                .replace(R.id.map, SupportMapFragment.newInstance()).commit();

        map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
                .getMap();
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(KOCAELI, 15));

    }
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108
  • Please provide full stacktrace of NPE – gio Dec 25 '14 at 15:23
  • 1
    The fragment transaction won't be finished when you are getting the map. You must wait for it to be ready if using fragment transactions or put the Map Fragment into your XML and then get it from onActivityCreated(). See this post http://stackoverflow.com/a/14047363/1296369 – kevskree Dec 25 '14 at 15:26

0 Answers0