0

I am trying to use a MapFragment inside one of my DrawerLayout item.

My DrawerLayout:

private void selectItem(int position) {
        switch (position) {
            (...)
            case 3:
                fragment = new MyMapFragment();
                break;
            (...)
            //THIS CODE WORKS WITH ANY OTHER FRAGMENT I CREATED!!!!!!
        }
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();

This code works fine with all my Fragments and never crash, except for the MyMapFragment. This one (only this one) crash on device rotation while the code is very similar to my other Fragments.

The only difference is that I am using a nested Fragment:

public class MyMapFragment extends Fragment {
 public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        mMapFragment = new MapFragment();

        GoogleMap map = mMapFragment.getMap();

        FragmentTransaction fragmentTransaction =
            getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.container, mMapFragment);
        fragmentTransaction.commit();
}

I don't what is wrong there...

Logcat:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*/com.*.MainActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.*.MyMapFragment$1: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • Check the import for MyMapFragment. Sometime it is due of a import mismatch (support library vs native support) – Blackbelt Jun 19 '13 at 13:06
  • debug this code in emulator And for screen rotation press `CTRL+F11`. And then paste your error stack trace. – mrsus Jun 19 '13 at 13:10
  • More importantly, you are attempting to use anonymous inner class extending `MapFragment`. That will not work. In fact, I can't quite see how this even compiles, as the code inside the `{}` after `new MapFragment()` is not contained in a method, and you are missing a few closing `}`. – CommonsWare Jun 19 '13 at 13:11

1 Answers1

1

extends FragmentActivity instead of MapFragement

Now use SupportMapFragment

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

map_layout.xml

<fragment
            android:id="@+id/map"

            android:name="com.google.android.gms.maps.SupportMapFragment" 

            android:layout_width="match_parent"
            android:layout_height="match_parent" />
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80